C# 窗体备份/还原数据库

很多人想实现在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对应恢复....

posted @ 2007-10-19 19:22  sunney  阅读(1133)  评论(0编辑  收藏  举报