ASP.NET中使用代码来进行备份和还原数据库

来源:互联网 发布:mac系统恢复被删除 编辑:程序博客网 时间:2024/06/03 17:10

Sql代码  收藏代码
  1. -- 备份数据库  
  2. backup database db_CSManage to disk='c:\backup.bak'  
  3. -- 还原数据库,必须先备份该数据库的日志文件到原先的备份文件中  
  4. backup log db_CSManage to disk='c:\backup.bak'  
  5. restore database db_CSManage from disk='c:\backup.bak'  

    其中db_CSManage是数据库名称,disk后的路径即是备份文件存储的路径。
知道了SQL语句,那么在.NET代码中就容易多了,备份的.NET代码如下:
C#代码  收藏代码
  1. try  
  2. {  
  3.     if (txtPath.Text != "" && txtName122.Text != "")  
  4.     {  
  5.         getSqlConnection geCon = new getSqlConnection();  
  6.         SqlConnection con = geCon.GetCon();  
  7.   
  8.         string strBacl = "backup database db_CSManage to disk='" + txtPath.Text.Trim() + "\\" + txtName.Text.Trim() + ".bak'";  
  9.         SqlCommand Cmd = new SqlCommand(strBacl, con);  
  10.         if (Cmd.ExecuteNonQuery() != 0)  
  11.         {  
  12.             MessageBox.Show("数据备份成功!""提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  13.             this.Close();  
  14.         }  
  15.         else  
  16.         {  
  17.             MessageBox.Show("数据备份失败!""提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  18.         }  
  19.   
  20.     }  
  21.     else  
  22.     {  
  23.         MessageBox.Show("请填写备份的正确位置及文件名!""提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  24.   
  25.     }// end   
  26. }  
  27. catch (Exception ee)  
  28. {  
  29.     MessageBox.Show(ee.Message.ToString());  
  30. }  

以下是还原数据库的代码,在示例中发现开头得先删除与该数据库相关的进程,然后在还原之前得先把数据库日志备份到开始的备份文件中,然后才还原备份文件,要不然会出错的,代码如下:
C#代码  收藏代码
  1. if (textPaht.Text != "")  
  2. {  
  3.     getSqlConnection geCon = new getSqlConnection();  
  4.     SqlConnection con = geCon.GetCon();  
  5.     if (con.State == ConnectionState.Open)  
  6.     {  
  7.         con.Close();  
  8.     }  
  9.     string DateStr = "Data Source=niunan\\sqlexpress;Database=master;User id=sa;PWD=123456";  
  10.     SqlConnection conn = new SqlConnection(DateStr);  
  11.     conn.Open();  
  12.   
  13.     //-------------------杀掉所有连接 db_CSManage 数据库的进程--------------  
  14.     string strSQL = "select spid from master..sysprocesses where dbid=db_id( 'db_CSManage') ";  
  15.     SqlDataAdapter Da = new SqlDataAdapter(strSQL, conn);  
  16.   
  17.     DataTable spidTable = new DataTable();  
  18.     Da.Fill(spidTable);  
  19.   
  20.     SqlCommand Cmd = new SqlCommand();  
  21.     Cmd.CommandType = CommandType.Text;  
  22.     Cmd.Connection = conn;  
  23.   
  24.     for (int iRow = 0; iRow <= spidTable.Rows.Count - 1; iRow++)  
  25.     {  
  26.         Cmd.CommandText = "kill " + spidTable.Rows[iRow][0].ToString();   //强行关闭用户进程   
  27.         Cmd.ExecuteNonQuery();  
  28.     }  
  29.     conn.Close();  
  30.     conn.Dispose();  
  31.     //--------------------------------------------------------------------  
  32.   
  33.     SqlConnection sqlcon = new SqlConnection(DateStr);  
  34.     sqlcon.Open();  
  35.     string sql = "backup log db_CSManage to disk='" + textPaht.Text.Trim() + "' restore database db_CSManage from disk='" + textPaht.Text.Trim() + "'";  
  36.     SqlCommand sqlCmd = new SqlCommand(sql, sqlcon);  
  37.     sqlCmd.ExecuteNonQuery();  
  38.     sqlCmd.Dispose();  
  39.     sqlcon.Close();  
  40.     sqlcon.Dispose();  
  41.     MessageBox.Show("数据还原成功!""提示", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  42.     MessageBox.Show("为了必免数据丢失,在数据库还原后将关闭整个系统。");  
  43.     Application.Exit();  
  44. }  
  45. else  
  46. {  
  47.     MessageBox.Show("请选择备份文件!""提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);  









http://niunan.iteye.com/blog/321062


0 0
原创粉丝点击