查看: 3872|回复: 0

[.NET开发] C#实现的SQL备份与还原功能示例

发表于 2018-2-18 20:05:34

本文实例讲述了C#实现的SQL备份与还原功能。分享给大家供大家参考,具体如下:

  1. //记得加 folderBrowserDialog1 openFileDialog1 控件
  2. using System.Data.SqlClient; //连接数据库 公共变量
  3. namespace WindowsApplication1.GoodMenhod
  4. {
  5. class getSqlConnection
  6. {
  7. string sql = "Data Source=win7-pc;database=Kc;uid=sa;pwd=sa";
  8. SqlConnection conn;
  9. public SqlConnection GetCon()
  10. {
  11. conn = new SqlConnection(sql);
  12. conn.Open();
  13. return conn;
  14. }
  15. }
  16. }
  17. using System.Data.SqlClient;
  18. using WindowsApplication1.GoodMenhod; //引用命名空间
  19. namespace WindowsApplication1
  20. {
  21. public partial class Form1 : Form
  22. {
  23. public Form1()
  24. {
  25. InitializeComponent();
  26. }
  27. private void button1_Click(object sender, EventArgs e) //打开 备份路径
  28. {
  29. if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
  30. {
  31. txtPath.Text = folderBrowserDialog1.SelectedPath.ToString();
  32. }
  33. }
  34. private void button2_Click(object sender, EventArgs e) //备份名称 保存
  35. {
  36. try
  37. {
  38. if (txtPath.Text != "" )
  39. {
  40. getSqlConnection geCon = new getSqlConnection();
  41. SqlConnection con = geCon.GetCon();
  42. string strBacl = "backup database Kc to disk='" + txtPath.Text.Trim() + "\\" + txtName.Text.Trim() + ".bak'";
  43. SqlCommand Cmd = new SqlCommand(strBacl, con);
  44. if (Cmd.ExecuteNonQuery() != 0)
  45. {
  46. MessageBox.Show("数据备份成功!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);
  47. this.Close();
  48. }
  49. else
  50. {
  51. MessageBox.Show("数据备份失败!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);
  52. }
  53. }
  54. else
  55. {
  56. MessageBox.Show("请填写备份的正确位置及文件名!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);
  57. }// end
  58. }
  59. catch (Exception ee)
  60. {
  61. MessageBox.Show(ee.Message.ToString());
  62. }
  63. }
  64. }
  65. }
  66. private void button3_Click(object sender, EventArgs e) //打开 将要还原的文件
  67. {
  68. openFileDialog1.FilterIndex = 0;
  69. openFileDialog1.FileName = "";
  70. openFileDialog1.Filter = "txt files (*.bak)|*.bak|All files (*.*)|*.*";
  71. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  72. {
  73. textPaht.Text = openFileDialog1.FileName.ToString();
  74. }
  75. }
  76. private void button4_Click(object sender, EventArgs e) //还原
  77. {
  78. if (textPaht.Text != "")
  79. {
  80. getSqlConnection geCon = new getSqlConnection();
  81. SqlConnection con = geCon.GetCon();
  82. if (con.State == ConnectionState.Open)
  83. {
  84. con.Close();
  85. }
  86. //连接的数据库是master,所以要初始化新的连接字符串
  87. string DateStr = "Data Source=win7-pc;Database=master;User id=sa;PWD=sa";
  88. SqlConnection conn = new SqlConnection(DateStr);
  89. conn.Open();
  90. //-------------------杀掉所有连接 db_CSManage 数据库的进程--------------
  91. // string sql = " SELECT spid FROM master..sysprocesses WHERE dbid=db_id('" + strDBName + "')";
  92. string strSQL = "select spid from master..sysprocesses where dbid=db_id( 'Kc') ";//读取连接当前数据库的进程
  93. SqlDataAdapter Da = new SqlDataAdapter(strSQL, conn);
  94. DataTable spidTable = new DataTable();
  95. Da.Fill(spidTable);
  96. SqlCommand Cmd = new SqlCommand();
  97. Cmd.CommandType = CommandType.Text;
  98. Cmd.Connection = conn;
  99. for (int iRow = 0; iRow <= spidTable.Rows.Count - 1; iRow++)
  100. {
  101. Cmd.CommandText = "kill " + spidTable.Rows[iRow][0].ToString(); //强行关闭用户进程
  102. Cmd.ExecuteNonQuery();
  103. }
  104. conn.Close();
  105. conn.Dispose();
  106. //--------------------------------------------------------------------
  107. SqlConnection sqlcon = new SqlConnection(DateStr);
  108. sqlcon.Open();
  109. SqlCommand sqlCmd = new SqlCommand("backup database Kc to disk='" + textPaht.Text.Trim() + "' restore database Kc from disk='" + textPaht.Text.Trim() + "'", sqlcon);
  110. sqlCmd.ExecuteNonQuery();
  111. sqlCmd.Dispose();
  112. sqlcon.Close();
  113. sqlcon.Dispose();
  114. MessageBox.Show("数据还原成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  115. MessageBox.Show("为了必免数据丢失,在数据库还原后将关闭整个系统。");
  116. Application.Exit();
  117. }
  118. else
  119. {
  120. MessageBox.Show("请选择备份文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  121. }
  122. }
复制代码

更多关于C#相关内容感兴趣的读者可查看本站专题:《C#常见控件用法教程》、《C#窗体操作技巧汇总》、《C#数据结构与算法教程》、《C#面向对象程序设计入门教程》及《C#程序设计之线程使用技巧总结》

希望本文所述对大家C#程序设计有所帮助。

您可能感兴趣的文章:

  • C#与SQL连接:GridView控件对数据库的操作
  • C#访问PostGreSQL数据库的方法
  • C#连接mysql数据库的方法
  • C#防SQL注入代码的三种方法
  • c#使用file.copy实现文件备份示例
  • c#连接sqlserver数据库、插入数据、从数据库获取时间示例
  • C#操作SQLite方法实例详解
  • C#访问SQLServer增删改查代码实例
  • C#实现较为实用的SQLhelper
  • 详解C#中SqlParameter的作用与用法
  • c#操作sql server2008 的界面实例代码
  • C#实现的sqlserver操作类实例


回复

使用道具 举报