SQLDMO数据库备份还原 带进度条
来源:互联网 发布:sql查询字段包含分号 编辑:程序博客网 时间:2024/05/17 03:49
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace SqlBack
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- //事先需要在窗体上放置一个ProgressBar 控件,命名为progressBar1
- //必须确保”D:\database\”路径名存在
- private SQLDMO.Backup backup; //声明用于数据库备份的对象
- private SQLDMO.Restore restore; //声明用于数据库还原的对象
- //备份数据库
- private void dataBackup()
- {
- backup = new SQLDMO.Backup();
- SQLDMO.SQLServer sqlServer = new SQLDMO.SQLServer();
- backup.PercentComplete += new SQLDMO.BackupSink_PercentCompleteEventHandler(backup_PercentComplete);//backup_PercentComplete
- try
- {
- progressBar1.Value = 0;
- label_percent.Location = new Point(76 + progressBar1.Value, label_percent.Location.Y);
- label_percent.Text = ”0%”;
- sqlServer.LoginSecure = false; //设置验证模式为SQL Server验证,true为混合验证
- sqlServer.Connect(”.”, “sa”, “123”); //连接的服务器地址 及用户名 密码
- backup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
- backup.Database = ”Timber_Exam_Biz_Group”; //要备份的数据库的名称
- backup.Files = ”D:\\backup.bak”;
- backup.BackupSetName = ”Timber_Exam_Biz_Group”;
- backup.BackupSetDescription = ”数据库备份”;
- backup.Initialize = true;
- backup.PercentCompleteNotification = 1; //备份进度每增加1%抛出一次PercentCompleteEventHandler事件
- this.Cursor = Cursors.AppStarting;
- backup.SQLBackup(sqlServer);//指定要执行备份操作的数据库连接
- this.Cursor = Cursors.Default;
- progressBar1.Value = 100;
- MessageBox.Show(”数据库备份完毕!”, “系统提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- catch (Exception excep)
- {
- MessageBox.Show(”数据库备份失败!请检查指定的备份路径及文件名是否合法。\n具体原因:\n” + excep.ToString(), “系统提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- finally
- {
- sqlServer.DisConnect();
- }
- backup.PercentComplete -= new SQLDMO.BackupSink_PercentCompleteEventHandler(backup_PercentComplete);
- backup = null;
- }
- //还原数据路
- private void dataRestore()
- {
- restore = new SQLDMO.Restore();
- SQLDMO.SQLServer sqlServer = new SQLDMO.SQLServer();
- restore.PercentComplete += new SQLDMO.RestoreSink_PercentCompleteEventHandler(backup_PercentComplete);
- try
- {
- progressBar1.Value = 0;
- label_percent.Location = new Point(76 + progressBar1.Value, label_percent.Location.Y);
- label_percent.Text = ”0%”;
- sqlServer.LoginSecure = false; //设置验证模式为SQL Server验证,true为混合验证
- sqlServer.Connect(”.”, “sa”, “123”); //连接的服务器地址
- restore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
- restore.Database = ”Timber_Exam_Biz_Group”;
- restore.Files = ”D:\\backup.bak”;
- restore.FileNumber = 1;
- restore.ReplaceDatabase = true;
- restore.PercentCompleteNotification = 1;//备份进度每增加1%抛出一次PercentCompleteEventHandler事件
- this.Cursor = Cursors.AppStarting; //鼠标样式变为指针加沙漏模式
- restore.SQLRestore(sqlServer);
- this.Cursor = Cursors.Default;
- progressBar1.Value = 100;
- MessageBox.Show(”数据库还原成功!”, “系统提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- catch (Exception restoreExce)
- {
- MessageBox.Show(”数据库还原失败!\n具体原因:\n” + restoreExce.ToString(), “系统提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- finally
- {
- sqlServer.DisConnect();
- }
- restore.PercentComplete -= new SQLDMO.RestoreSink_PercentCompleteEventHandler(backup_PercentComplete);
- restore = null;
- }
- //根据当前备份操作的完成进度显示进度条
- private void backup_PercentComplete(string message, int percent)
- {
- /*这里要特别说明一下,取出当前数据库备份/还原操作完成进度百分比的方法
- *网上几乎所有的代码示例都直接去percent的值 ,然后把它赋给progress1.value,
- *但是我试了很多种方法,发现备份操作无论调用多少次backup_PercentComplete,
- *percent的值一直都是0,不知道是我的数据库设置的原因还是什么,MSDN里面没有关于SQLDMO的讲解
- *网上也找不到解决的办法,后来自己测试发现在第一个参数message中包含有percent信息,至于包含在哪个地方
- *可以自己输出测试一下。 我下面用到的value值就是从message中去出来,不管怎么说,功能总算是实现了。
- */
- char[] a = new char[] { ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’ };
- int startPos = message.IndexOfAny(a);
- int endPos = message.LastIndexOfAny(a);
- int value = Convert.ToInt32(message.Substring(startPos, endPos - startPos + 1));
- progressBar1.Value = value;
- label_percent.Location = new Point(73 + progressBar1.Value * progressBar1.Size.Width / 100, label_percent.Location.Y);
- label_percent.Text = progressBar1.Value.ToString() + ”%”;
- Application.DoEvents();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- dataBackup();
- }
- }
- }
写代码之前请做好准备工作,请参见: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
- SQLDMO数据库备份还原 带进度条
- SQLDMO数据库备份还原 带进度条
- SQLDMO备份,还原数据库
- 用SQLDMO备份还原数据库
- 使用SQLDMO进行数据库备份与还原
- 利用SQLDMO备份和还原数据库
- C#备份还原数据库(sqldmo.dll篇)
- C#中结合使用SQLDMO实现备份、还原SQLserver数据库
- Asp.net 不使用SQLDMO实现数据库备份和还原
- C#中结合使用SQLDMO实现备份、还原SQLserver数据库
- Asp.net 不使用SQLDMO实现数据库备份和还原
- C#中结合使用SQLDMO实现备份、还原SQLserver数据库
- Asp.net 不使用SQLDMO实现数据库备份和还原
- c# 用SQLDMO 进行sql数据库备份及还原
- C#实现SQLSERVER2000数据库备份还原的两种方法 (带进度条)
- SQLDMO数据库备份
- 用SQLDMO在ASP.NET中备份和还原数据库(SQLSERVER2000)
- WPF C#开发中利用SQLDMO进行数据库备份还原操作中的注意点
- 十大算法
- 更换kernel启动logo图片
- android Intent : android.intent.category.DEFAULT的用途和使用
- 423. Reconstruct Original Digits from English**
- 文章标题
- SQLDMO数据库备份还原 带进度条
- 循环List删除元素抛ConcurrentModificationException异常原因分析
- RSA&AES实现可靠通信
- Android使用DrawerLayout实现抽屉式侧滑菜单
- iOS 硬件授权检测:定位服务、通讯录、日历、提醒事项、照片、蓝牙共享、麦克风、相机等
- 洛谷 P1032 字串变换
- 清除浮动的几种方法
- tc命令——Linux基于IP进行流量限速
- 收藏本网站代码