zoukankan      html  css  js  c++  java
  • 在ASP.NET中备份和还原数据库

        昨天看了《C#项目实录》中的进销存管理系统,和其他书里讲的案例一样,无非也就是数据库增删查改,但是这个进销存系统中有一个备份和还原数据库的功能,蛮有兴趣的,看了一下代码,原来如此,也就是通过SQL语句来进行备份和还原数据库,SQL语句如下: 
    Sql代码  收藏代码
    1. -- 备份数据库  
    2. backup database db_CSManage to disk='c:ackup.bak'  
    3. -- 还原数据库,必须先备份该数据库的日志文件到原先的备份文件中  
    4. backup log db_CSManage to disk='c:ackup.bak'  
    5. restore database db_CSManage from disk='c:ackup.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);  
    48. }  

        附上整个进销存系统的源码
  • 相关阅读:
    用c和c++的方式实现栈
    类的static成员并用其实现一个单例模式
    windows安装mongodb
    centos 安装beanstalkd
    使用 OAuth2-Server-php 在 Yii 框架上搭建 OAuth2 Server
    Git Windows客户端保存用户名与密码
    PHP 输入流 php://input
    yii accessRules用法
    php curl
    yii 初步安装
  • 原文地址:https://www.cnblogs.com/ranran/p/3891837.html
Copyright © 2011-2022 走看看