Advertisement
Guest User

Untitled

a guest
Jul 25th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. //to do DB backup
  2.  
  3. private void spid2_Click(object sender, EventArgs e)
  4. {
  5. string SQLDataBases;
  6. SQLDataBases = "select @@spid ";
  7. SQLDataBases += "BACKUP DATABASE School TO DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Backup\AdventureWorks333.BAK' ";
  8. string svr = "Server=" + localsrv + ";Initial Catalog=master;Integrated Security = SSPI;";
  9. SqlConnection cnBk = new SqlConnection(svr);
  10. Command = new SqlCommand(SQLDataBases, cnBk);
  11. Command.CommandText = SQLDataBases;
  12. SqlDataAdapter da = new SqlDataAdapter(Command);
  13. DataTable dtDatabases = new DataTable();
  14.  
  15. try
  16. {
  17. cnBk.Open();
  18. da.Fill(dtDatabases);
  19. label1.Text = dtDatabases.Rows[0][0].ToString();
  20. }
  21.  
  22. catch (Exception ex)
  23. {
  24. string s = ex.ToString();
  25. MessageBox.Show(s);
  26. label1.Text = dtDatabases.Rows[0][0].ToString();
  27. }
  28.  
  29. finally
  30. {
  31. if (cnBk.State == ConnectionState.Open)
  32. {
  33. cnBk.Close();
  34. cnBk.Dispose();
  35.  
  36. }
  37. }
  38. }
  39.  
  40.  
  41. //to kill backup session
  42.  
  43.  
  44. private void kill_Click(object sender, EventArgs e)
  45. {
  46. string SQLRestor;
  47.  
  48. SQLRestor = "Use master; kill " + label1.Text;
  49. string svr = "Server=" + localsrv + ";Initial Catalog=master;Integrated Security = SSPI;";
  50.  
  51. SqlConnection cnRestore = new SqlConnection(svr);
  52. SqlCommand cmdBkUp = new SqlCommand(SQLRestor, cnRestore);
  53.  
  54. try
  55. {
  56. cnRestore.Open();
  57. cmdBkUp.ExecuteNonQuery();
  58. }
  59.  
  60. catch (Exception ex)
  61. {
  62. string s = ex.ToString();
  63. }
  64.  
  65. finally
  66. {
  67. if (cnRestore.State == ConnectionState.Open)
  68. {
  69. cnRestore.Close();
  70. cnRestore.Dispose();
  71. }
  72. }
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement