SQLDMO数据库备份还原 带进度条

来源:互联网 发布:sql查询字段包含分号 编辑:程序博客网 时间:2024/05/17 03:49
[csharp] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.   
  10. namespace SqlBack  
  11. {  
  12.     public partial class Form1 : Form  
  13.     {  
  14.         public Form1()  
  15.         {  
  16.             InitializeComponent();  
  17.         }  
  18.   
  19.   
  20.   
  21.   
  22.         //事先需要在窗体上放置一个ProgressBar 控件,命名为progressBar1  
  23.         //必须确保”D:\database\”路径名存在  
  24.         private SQLDMO.Backup backup; //声明用于数据库备份的对象  
  25.         private SQLDMO.Restore restore; //声明用于数据库还原的对象  
  26.   
  27.   
  28.         //备份数据库  
  29.         private void dataBackup()  
  30.         {  
  31.             backup = new SQLDMO.Backup();  
  32.             SQLDMO.SQLServer sqlServer = new SQLDMO.SQLServer();  
  33.             backup.PercentComplete += new SQLDMO.BackupSink_PercentCompleteEventHandler(backup_PercentComplete);//backup_PercentComplete  
  34.             try  
  35.             {  
  36.                 progressBar1.Value = 0;  
  37.                 label_percent.Location = new Point(76 + progressBar1.Value, label_percent.Location.Y);  
  38.                 label_percent.Text = ”0%”;  
  39.                 sqlServer.LoginSecure = false;      //设置验证模式为SQL Server验证,true为混合验证  
  40.                 sqlServer.Connect(”.”“sa”“123”);    //连接的服务器地址 及用户名 密码  
  41.                 backup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;  
  42.                 backup.Database = ”Timber_Exam_Biz_Group”;      //要备份的数据库的名称  
  43.                 backup.Files = ”D:\\backup.bak”;  
  44.                 backup.BackupSetName = ”Timber_Exam_Biz_Group”;  
  45.                 backup.BackupSetDescription = ”数据库备份”;  
  46.                 backup.Initialize = true;  
  47.                 backup.PercentCompleteNotification = 1;  //备份进度每增加1%抛出一次PercentCompleteEventHandler事件  
  48.                 this.Cursor = Cursors.AppStarting;  
  49.                 backup.SQLBackup(sqlServer);//指定要执行备份操作的数据库连接  
  50.                 this.Cursor = Cursors.Default;  
  51.                 progressBar1.Value = 100;  
  52.   
  53.                 MessageBox.Show(”数据库备份完毕!”“系统提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);  
  54.             }  
  55.             catch (Exception excep)  
  56.             {  
  57.                 MessageBox.Show(”数据库备份失败!请检查指定的备份路径及文件名是否合法。\n具体原因:\n” + excep.ToString(), “系统提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);  
  58.             }  
  59.             finally  
  60.             {  
  61.                 sqlServer.DisConnect();  
  62.             }  
  63.             backup.PercentComplete -= new SQLDMO.BackupSink_PercentCompleteEventHandler(backup_PercentComplete);  
  64.             backup = null;  
  65.         }  
  66.   
  67.         //还原数据路  
  68.         private void dataRestore()  
  69.         {  
  70.             restore = new SQLDMO.Restore();  
  71.             SQLDMO.SQLServer sqlServer = new SQLDMO.SQLServer();  
  72.             restore.PercentComplete += new SQLDMO.RestoreSink_PercentCompleteEventHandler(backup_PercentComplete);  
  73.             try  
  74.             {  
  75.                 progressBar1.Value = 0;  
  76.                 label_percent.Location = new Point(76 + progressBar1.Value, label_percent.Location.Y);  
  77.                 label_percent.Text = ”0%”;  
  78.                 sqlServer.LoginSecure = false;      //设置验证模式为SQL Server验证,true为混合验证  
  79.                 sqlServer.Connect(”.”“sa”“123”);    //连接的服务器地址  
  80.                 restore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;  
  81.                 restore.Database = ”Timber_Exam_Biz_Group”;  
  82.                 restore.Files = ”D:\\backup.bak”;  
  83.                 restore.FileNumber = 1;  
  84.                 restore.ReplaceDatabase = true;  
  85.                 restore.PercentCompleteNotification = 1;//备份进度每增加1%抛出一次PercentCompleteEventHandler事件  
  86.                 this.Cursor = Cursors.AppStarting; //鼠标样式变为指针加沙漏模式  
  87.                 restore.SQLRestore(sqlServer);  
  88.                 this.Cursor = Cursors.Default;  
  89.                 progressBar1.Value = 100;  
  90.                 MessageBox.Show(”数据库还原成功!”“系统提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);  
  91.             }  
  92.             catch (Exception restoreExce)  
  93.             {  
  94.                 MessageBox.Show(”数据库还原失败!\n具体原因:\n” + restoreExce.ToString(), “系统提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);  
  95.             }  
  96.             finally  
  97.             {  
  98.                 sqlServer.DisConnect();  
  99.             }  
  100.             restore.PercentComplete -= new SQLDMO.RestoreSink_PercentCompleteEventHandler(backup_PercentComplete);  
  101.             restore = null;  
  102.         }  
  103.   
  104.         //根据当前备份操作的完成进度显示进度条  
  105.         private void backup_PercentComplete(string message, int percent)  
  106.         {  
  107.             /*这里要特别说明一下,取出当前数据库备份/还原操作完成进度百分比的方法 
  108.              *网上几乎所有的代码示例都直接去percent的值 ,然后把它赋给progress1.value, 
  109.              *但是我试了很多种方法,发现备份操作无论调用多少次backup_PercentComplete, 
  110.              *percent的值一直都是0,不知道是我的数据库设置的原因还是什么,MSDN里面没有关于SQLDMO的讲解 
  111.              *网上也找不到解决的办法,后来自己测试发现在第一个参数message中包含有percent信息,至于包含在哪个地方 
  112.              *可以自己输出测试一下。 我下面用到的value值就是从message中去出来,不管怎么说,功能总算是实现了。 
  113.              */  
  114.             char[] a = new char[] { ‘0’‘1’‘2’‘3’‘4’‘5’‘6’‘7’‘8’‘9’ };  
  115.             int startPos = message.IndexOfAny(a);  
  116.             int endPos = message.LastIndexOfAny(a);  
  117.             int value = Convert.ToInt32(message.Substring(startPos, endPos - startPos + 1));  
  118.             progressBar1.Value = value;  
  119.             label_percent.Location = new Point(73 + progressBar1.Value * progressBar1.Size.Width / 100, label_percent.Location.Y);  
  120.             label_percent.Text = progressBar1.Value.ToString() + ”%”;  
  121.             Application.DoEvents();  
  122.   
  123.         }  
  124.   
  125.         private void button1_Click(object sender, EventArgs e)  
  126.         {  
  127.             dataBackup();  
  128.         }  
  129.   
  130.   
  131.     }  
  132. }  

 

 

写代码之前请做好准备工作,请参见:http://blog.csdn.net/CatchMe_439/article/details/54613678

之后还会报错“event invocation for COM objects requires event to be attributed with DispIdAttribute””

解决办法:

这个原因是在项目Bin中引用SQLDMO.dll文件后,该dll属性中的“嵌入互操作类型”的值默认是True造成的,修改成False即可。

\

1 0
原创粉丝点击