zoukankan      html  css  js  c++  java
  • ASP.NET中使用代码来进行备份和还原数据库

     SQL代码:

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

    其中db_CSManage是数据库名称,disk后的路径即是备份文件存储的路径。 
    知道了SQL语句,那么在.NET代码中就容易多了,备份的.NET代码如下:

    C#代码-备份: 

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    try
    {
        if (txtPath.Text != "" && txtName122.Text != "")
        {
            getSqlConnection geCon = new getSqlConnection();
            SqlConnection con = geCon.GetCon();
     
            string strBacl = "backup database db_CSManage to disk='" + txtPath.Text.Trim() + "\" + txtName.Text.Trim() + ".bak'";
            SqlCommand Cmd = new SqlCommand(strBacl, con);
            if (Cmd.ExecuteNonQuery() != 0)
            {
                MessageBox.Show("数据备份成功!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            else
            {
                MessageBox.Show("数据备份失败!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
     
        }
        else
        {
            MessageBox.Show("请填写备份的正确位置及文件名!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);
     
        }// end
    }
    catch (Exception ee)
    {
        MessageBox.Show(ee.Message.ToString());
    }

    以下是还原数据库的代码,在示例中发现开头得先删除与该数据库相关的进程,然后在还原之前得先把数据库日志备份到开始的备份文件中,然后才还原备份文件,要不然会出错的,代码如下:  

    C#代码-还原: 

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    if (textPaht.Text != "")
    {
        getSqlConnection geCon = new getSqlConnection();
        SqlConnection con = geCon.GetCon();
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        string DateStr = "Data Source=niunan\sqlexpress;Database=master;User id=sa;PWD=123456";
        SqlConnection conn = new SqlConnection(DateStr);
        conn.Open();
     
        //-------------------杀掉所有连接 db_CSManage 数据库的进程--------------
        string strSQL = "select spid from master..sysprocesses where dbid=db_id( 'db_CSManage') ";
        SqlDataAdapter Da = new SqlDataAdapter(strSQL, conn);
     
        DataTable spidTable = new DataTable();
        Da.Fill(spidTable);
     
        SqlCommand Cmd = new SqlCommand();
        Cmd.CommandType = CommandType.Text;
        Cmd.Connection = conn;
     
        for (int iRow = 0; iRow <= spidTable.Rows.Count - 1; iRow++)
        {
            Cmd.CommandText = "kill " + spidTable.Rows[iRow][0].ToString();   //强行关闭用户进程
            Cmd.ExecuteNonQuery();
        }
        conn.Close();
        conn.Dispose();
        //--------------------------------------------------------------------
     
        SqlConnection sqlcon = new SqlConnection(DateStr);
        sqlcon.Open();
        string sql = "backup log db_CSManage to disk='" + textPaht.Text.Trim() + "' restore database db_CSManage from disk='" + textPaht.Text.Trim() + "'";
        SqlCommand sqlCmd = new SqlCommand(sql, sqlcon);
        sqlCmd.ExecuteNonQuery();
        sqlCmd.Dispose();
        sqlcon.Close();
        sqlcon.Dispose();
        MessageBox.Show("数据还原成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        MessageBox.Show("为了必免数据丢失,在数据库还原后将关闭整个系统。");
        Application.Exit();
    }
    else
    {
        MessageBox.Show("请选择备份文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    }
  • 相关阅读:
    汉语-词语:架构(计算机术语)
    汉语-词语:架构
    全世界云计算宕机和中断[2013年-2014年集锦]
    java异常处理Exception
    CodeForces 390E Inna and Large Sweet Matrix(树状数组改段求段)
    操作系统CPU调度知识点
    hp-ux 集群,内存 小记
    增强for循环、Map接口遍历、可变參数方法
    spring下载和安装
    Android设计模式(八)--模板方法模式
  • 原文地址:https://www.cnblogs.com/zxtceq/p/5900700.html
Copyright © 2011-2022 走看看