在使用SQLDMO备份数据库的过程中,参考了网上的例子,但是进度条却不显示,每次返回的都是0,解决方法是使用全局变量,每次做个累加就可以了。
string ServerName = ""; string UserName = "sa";//暂时锁定,可以根据需要自己设置 string Password = "sa"; string DataPath = "E:\\数据库备份\\"; //数据库备份路径 bool successFlag = false; //备份是否成功的标志 int step = 1; //进度条 private void Form1_Load(object sender, EventArgs e) { CheckForIllegalCrossThreadCalls = false; } //获取数据库列表 public ArrayList GetDbList(string strServerName, string strUserName, string strPwd) { string ServerName = strServerName; string UserName = strUserName; string Password = strPwd; ArrayList alDbs = new ArrayList(); SQLDMO.Application sqlApp = new SQLDMO.ApplicationClass(); SQLDMO.SQLServer svr = new SQLDMO.SQLServerClass(); try { svr.Connect(ServerName, UserName, Password); foreach (SQLDMO.Database db in svr.Databases) { if (db.Name != null) alDbs.Add(db.Name); L_data.Items.Add(db.Name); } } catch (Exception e) { MessageBox.Show("连接数据库出错:" + e.Message); } finally { svr.DisConnect(); sqlApp.Quit(); } return alDbs; } //备份数据 public bool BackUPDB(string strDbName, string strFileName, ProgressBar pgbMain) { ProgressBar PBar = pgbMain; SQLDMO.SQLServer svr = new SQLDMO.SQLServerClass(); try { svr.Connect(ServerName, UserName, Password); SQLDMO.Backup bak = new SQLDMO.BackupClass(); bak.Action = 0; bak.Initialize = true; SQLDMO.BackupSink_PercentCompleteEventHandler pceh = new SQLDMO.BackupSink_PercentCompleteEventHandler(Step); bak.PercentComplete += pceh; bak.Files = strFileName;//这里可以写成路径+文件名形式,自己写! bak.Database = strDbName; bak.SQLBackup(svr); L1.Items.Add(DateTime.Now.ToShortDateString() + " 数据库备份完成,位置为:" + strFileName); PBar.Value = 0; successFlag = true; return true; } catch (Exception err) { throw (new Exception("备份数据库失败" + err.Message)); L1.Items.Add(DateTime.Now.ToShortDateString() + " 数据库备份失败!"); } finally { svr.DisConnect(); } } //进度条,precent返回值是0,备份完成会返回10次,所以这里用step来计数,然后乘10给进度条 private void Step(string message, int percent) { PBar.Value = step*10; step += 1; } private void btn_load_Click(object sender, EventArgs e) { //显示数据库 ServerName = txt_IP.Text.Trim().ToString(); UserName = txt_user.Text.Trim().ToString(); Password = txt_pass.Text.Trim().ToString(); if (UserName.Length < 2) { MessageBox.Show("请输入正确的数据库用户名"); return; } if (Password.Length < 1) { MessageBox.Show("请输入正确的数据库密码,不支持空密码输入"); return; } GetDbList(ServerName, UserName, Password); //检查备份路径是否存在 try { if (!Directory.Exists(DataPath)) { DirectoryInfo di = Directory.CreateDirectory(DataPath); } } catch (Exception ex) { MessageBox.Show("创建目录失败:{0}", ex.ToString()); } } private void button1_Click(object sender, EventArgs e) { //备份数据库 string DataName = L_data.SelectedItem.ToString(); string Filename = DataPath + DataName + "_" + DateTime.Now.ToShortDateString(); L1.Items.Add(DateTime.Now.ToShortDateString() + " 选择了"+DataName+"进行备份!"); successFlag = false; step = 1; Thread t1 = new Thread(() => BackUPDB(DataName, Filename, PBar)); t1.IsBackground = true; t1.Start(); }
标签:string,SQLDMO,C#,备份,new,svr,数据库 From: https://www.cnblogs.com/wjbych/p/17061379.html