在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
本文转载自:http://www.cnblogs.com/terrylee/articles/253667.html 原文作者:
首先需要添加对SQLDMO引用 1.实现数据库的备份:
1 }
2.实现数据库恢复: 在恢复时要注意先杀掉当前数据库的所有进程
1 }
完整的操作类如下:
1using System;
2using System.Collections; 3using System.Data; 4using System.Data.SqlClient; 5 6namespace DbBackUp 7 在相应的按钮
1<asp:Button id="wbtn_Backup" runat="server" Width="60px" Text="备 份" CssClass="Button"></asp:Button>
单击事件里调用即可:
1 }
^_^ TerryLee
出处:http://terrylee.cnblogs.com 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 个人对上面的版本做了一些小的修整,谢谢李会军作出的共享.
using System;
using System.Collections.Generic; using System.Text; using System.Data; using System.Data.SqlClient; namespace WindowsApp { /// <summary> /// 功能描述:实现SQL Server 2005数据库的备份和恢复 /// </summary> public class DBOperate { /// <summary> /// 服务器 /// </summary> private string _server; /// <summary> /// 服务器 /// </summary> public string Server { get { return _server; } set { _server = value; } } /// <summary> /// 登录名 /// </summary> private string _uid; /// <summary> /// 登录名 /// </summary> public string Uid { get { return _uid; } set { _uid = value; } } /// <summary> /// 登录密码 /// </summary> private string _pwd; /// <summary> /// 登录密码 /// </summary> public string Pwd { get { return _pwd; } set { _pwd = value; } } /// <summary> /// 数据库 /// </summary> private string _database; /// <summary> /// 数据库 /// </summary> public string Database { get { return _database; } set { _database = value; } } /// <summary> /// 连接字符串 /// </summary> private string _conn; /// <summary> /// 连接字符串 /// </summary> public string Conn { get { return _conn; } set { _conn = value; } } /// <summary> /// 默认构造函数 /// </summary> public DBOperate() { } /// <summary> /// 构造函数 /// </summary> /// <param name="database">数据库</param> /// <param name="uid">登录名</param> /// <param name="pwd">登录密码</param> /// <param name="server">服务器</param> public DBOperate(string database,string uid,string pwd,string server) { this._server = server; this._uid = uid; this._pwd = pwd; this._database = database; } /// <summary> /// 切割字符串 /// </summary> /// <param name="str"></param> /// <param name="bg"></param> /// <param name="ed"></param> /// <returns></returns> public string StringCut(string str, string bg, string ed) { string sub; sub = str.Substring(str.IndexOf(bg) + bg.Length); sub = sub.Substring(0, sub.IndexOf(";")); return sub; } /// <summary> /// 构造文件名 /// </summary> /// <returns>文件名</returns> private string CreatePath() { string currTime = System.DateTime.Now.ToString(); currTime = currTime.Replace("-", ""); currTime = currTime.Replace(":", ""); currTime = currTime.Replace(" ", ""); currTime = currTime.Substring(0, 12); string path = @"d:\\aaa\\"; path += _database; path += "_db_"; path += currTime; path += ".BAK"; return path; } /// <summary> /// 数据库备份 /// </summary> /// <param name="file">备份文件的完整存放路径,包括文件名</param> /// <returns>备份是否成功</returns> public bool DBBackup(string file) { //string path = CreatePath(); SQLDMO.Backup backup = new SQLDMO.BackupClass(); SQLDMO.SQLServer sqlserver = new SQLDMO.SQLServerClass(); try { sqlserver.LoginSecure = false; sqlserver.Connect(_server, _uid, _pwd); backup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database; backup.Database = _database; backup.Files = file; backup.BackupSetName = _database; backup.BackupSetDescription = "数据库备份"; backup.Initialize = true; backup.SQLBackup(sqlserver); return true; } catch (Exception ex) { throw ex; } finally { sqlserver.DisConnect(); } } /// <summary> /// 数据库恢复 /// </summary> /// <param name="file">备份文件的完整存放路径,包括文件名</param> /// <returns></returns> public string DBRestore(string file) { SQLDMO.Restore restore = new SQLDMO.RestoreClass(); SQLDMO.SQLServer sqlserver = new SQLDMO.SQLServerClass(); try { killProcess(_database); sqlserver.LoginSecure = false; sqlserver.Connect(_server, _uid, _pwd); restore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database; restore.Database = _database; //自行修改 restore.Files = file; restore.FileNumber = 1; restore.ReplaceDatabase = true; restore.SQLRestore(sqlserver); return "ok"; } catch (Exception ex) { throw ex; } finally { sqlserver.DisConnect(); } } /// <summary> /// 杀死当前库的所有进程 /// </summary> /// <param name="dbName">数据库名称</param> /// <returns></returns> private bool killProcess(string dbName) { SqlConnection conn = new SqlConnection("server=" + _server + ";uid=" + _uid + ";pwd=" + _pwd + ";database=master"); SqlCommand comm = new SqlCommand("killspid", conn); comm.CommandType = CommandType.StoredProcedure; comm.Parameters.AddWithValue("@dbname", dbName); try { conn.Open(); comm.ExecuteNonQuery(); return true; } catch (Exception ex) { throw ex; } finally { conn.Close(); } } } } 使用范例如下:
private void backup_Click(object sender, EventArgs e)
{ DBOperate dbOperate = new DBOperate("DevTest", "sa", "123", "sql2005"); try { if (dbOperate.DBBackup(@"d:\123.bak")) { MessageBox.Show("备份成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { MessageBox.Show("数据库备份失败,失败原因:" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } private void restore_Click(object sender, EventArgs e) { DBOperate dbOperate = new DBOperate("DevTest", "sa", "123", @"sql2005"); try { if (dbOperate.DBRestore(@"d:\123.bak").Equals("ok")) { MessageBox.Show("恢复成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { MessageBox.Show("数据库恢复失败,失败原因:" + ex.Message,"提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } 程序中使用的存储过程killspid如下:
use master
go --------------------------------------- --关闭指定数据库的全部访问进程 --------------------------------------------- create proc killspid @dbname varchar(200) --要关闭进程的数据库名 as declare @sql nvarchar(500) declare @spid nvarchar(20) declare #tb cursor for select spid=cast(spid as varchar(20)) from master..sysprocesses where dbid=db_id(@dbname) open #tb fetch next from #tb into @spid while @@fetch_status=0 begin exec('kill '+@spid) fetch next from #tb into @spid end close #tb deallocate #tb go |
请发表评论