功能:
自动识别Web.Config配置信息.
备份与还原数据库.
代码如下:
- using System;
- using System.Configuration;
- using System.Data.SqlClient;
- using System.Data;
- namespace OA.Web
- {
- /// <summary>
- /// SqlDbBackUpRestore类,主要应用SQLDMO实现对Microsoft SQL Server数据库 的备份和恢复
- /// 请先给项目添加COM(Mircrosoft SQLDMO Object Library)引用
- /// </summary>
- public class SqlDbBackUpRestore
- {
- /// <summary>
- /// sql服务器名称或者IP地址
- /// </summary>
- private string server;
- /// <summary>
- ///用户名称
- /// </summary>
- private string uid;
- /// <summary>
- /// 用户密码
- /// </summary>
- private string pwd;
- /// <summary>
- /// 数据库名称
- /// </summary>
- private string database;
- /// <summary>
- /// Web.Config连接字符串
- /// </summary>
- private string conn;
- /// <summary>
- /// SqlDbBackUpRestore类的构造函数
- /// </summary>
- public SqlDbBackUpRestore()
- {
- conn = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString();
- server = cut(conn, "server=", ";");
- uid = cut(conn, "uid=", ";");
- pwd = cut(conn, "pwd=", ";");
- database = cut(conn, "database=", ";");
- }
- public string cut(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>
- /// 数据库database备份
- /// </summary>
- public bool DbBackup(string url)
- {
- SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
- SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
- try
- {
- oSQLServer.LoginSecure = false;
- oSQLServer.Connect(server, uid, pwd);
- oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
- oBackup.Database = database;
- oBackup.Files = url;//"d:/Northwind.bak";
- oBackup.BackupSetName = database;
- oBackup.BackupSetDescription = "数据库备份";
- oBackup.Initialize = true;
- oBackup.SQLBackup(oSQLServer);
- return true;
- }
- catch
- {
- return false;
- throw;
- }
- finally
- {
- oSQLServer.DisConnect();
- }
- }
- /// <summary>
- /// 数据库database恢复
- /// </summary>
- public string DbRestore(string url)
- {
- if (KillProcesses() != true)//执行存储过程
- {
- return "操作失败";
- }
- else
- {
- SQLDMO.Restore oRestore = new SQLDMO.RestoreClass();
- SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
- try
- {
- oSQLServer.LoginSecure = false;
- oSQLServer.Connect(server, uid, pwd);
- oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
- oRestore.Database = database;
- oRestore.Files = url;//@"d:/Northwind.bak";
- oRestore.FileNumber = 1;
- oRestore.ReplaceDatabase = true;
- oRestore.SQLRestore(oSQLServer);
- return "OK";
- }
- catch (Exception e)
- {
- return "恢复数据库失败";
- throw;
- }
- finally
- {
- oSQLServer.DisConnect();
- }
- }
- }
- /// <summary>
- /// 杀死Sql database 连接进程
- /// </summary>
- /// <returns></returns>
- private bool KillProcesses()
- {
- SQLDMO.SQLServer svr = new SQLDMO.SQLServerClass();
- try
- {
- svr.Connect(server, uid, pwd);
- SQLDMO.QueryResults qr = svr.EnumProcesses(-1);
- int iColPIDNum = -1;
- int iColDbName = -1;
- for (int i = 1; i <= qr.Columns; i++)
- {
- string strName = qr.get_ColumnName(i);
- if (strName.ToUpper().Trim() == "SPID")
- {
- iColPIDNum = i;
- }
- else if (strName.ToUpper().Trim() == "DBNAME")
- {
- iColDbName = i;
- }
- if (iColPIDNum != -1 && iColDbName != -1)
- break;
- }
- for (int i = 1; i <= qr.Rows; i++)
- {
- int lPID = qr.GetColumnLong(i, iColPIDNum);
- string strDBName = qr.GetColumnString(i, iColDbName);
- if (strDBName.ToUpper() == database.ToUpper())
- {
- svr.KillProcess(lPID);
- }
- }
- return true;
- }
- catch
- {
- //失败
- return false;
- }
- finally
- {
- svr.DisConnect();
- }
- }
- }
- }