c#备份恢复sql数据库

 很多人想实现在c#中备份与恢复sql数据库,又想要有在sql中那种进度条的状态.其实用c#的sqldmo就可以实现

//数据库备份
        string backaway =textbox1.Text.Trim();
            SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
            SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
            try
            {
                oSQLServer.LoginSecure = false;
                //下面设置登录sql服务器的ip,登录名,登录密码
                oSQLServer.Connect(serverip, serverid, serverpwd);
                oBackup.Action = 0;
               //下面两句是显示进度条的状态
                SQLDMO.BackupSink_PercentCompleteEventHandler pceh = new SQLDMO.BackupSink_PercentCompleteEventHandler(Step2);
                oBackup.PercentComplete += pceh;
                //数据库名称:
                oBackup.Database = "k2";
                //备份的路径
                oBackup.Files = @backaway;
                //备份的文件名
                oBackup.BackupSetName = "k2";
                oBackup.BackupSetDescription = "数据库备份";
                oBackup.Initialize = true;
                oBackup.SQLBackup(oSQLServer);
                MessageBox.Show("备份成功!", "提示");
            }
            catch
            {
                MessageBox.Show("备份失败!", "提示");
            }
            finally
            {
                oSQLServer.DisConnect();
            }



//数据库恢复
        //获取恢复的路径
        string dbaway = textbox2.Text.Trim();
            SQLDMO.Restore restore = new SQLDMO.RestoreClass();
            SQLDMO.SQLServer server = new SQLDMO.SQLServerClass();
            server.Connect(serverip, serverid, serverpwd);

            //KILL DataBase Process
            conn = new 工资管理系统.CCUtility.connstring();
            conn.DBOpen();
            SqlCommand cmd = new SqlCommand("use master Select spid FROM sysprocesses ,sysdatabases Where sysprocesses.dbid=sysdatabases.dbid AND sysdatabases.Name='k2'", conn.Connection);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                server.KillProcess(Convert.ToInt32(dr[0].ToString()));
            }
            dr.Close();
            conn.DBClose();

            try
            {
                restore.Action = 0;
                SQLDMO.RestoreSink_PercentCompleteEventHandler pceh = new SQLDMO.RestoreSink_PercentCompleteEventHandler(Step);
                restore.PercentComplete += pceh;
                restore.Database = "k2";
                restore.Files = @dbaway;
                restore.ReplaceDatabase = true;
                restore.SQLRestore(server);
                MessageBox.Show("数据库恢复成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                server.DisConnect();
            }

恢复相关的参数和备份相同,不再解释,自己看一下.

上面两个函数调用到了更改进度条的两个函数:

       private void Step2(string message, int percent)
        {
            progressBar2.Value = percent;
        }

        private void Step(string message, int percent)
        {
            progressBar1.Value = percent;
        }

setp对应备份,,setp2对应恢复....

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值