在这里我通过在程序中把sql server数据库安装到客户的机器上去:在还原数据前先检查是否有该数据库,如果有,可以删除,也可以杀掉该数据库相关进程。然后再执行还原操作。
Code
public class DatabaseOper
{
public DataOperateBase _Oper;//数据库操作类对象
public DatabaseOper()
{
_Oper = new SqlDataOperate();
//WebConfig 为定义类
_Oper.ConnectionString = WebConfig.getMasterConn();//初始化连接
}
/// <summary>
/// 备份/恢复
/// </summary>
/// <param name="Type">操作类型,1为备份,2为恢复</param>
/// <param name="FilePath">路径,如D:\Bak\Bak.bak</param>
/// <returns></returns>
protected bool DatabaseBak(int Type, string FilePath)
{
bool ReturnData = false;
string dbName;
string sqlText = string.Empty;
SqlConnection con = new SqlConnection();
con.ConnectionString = WebConfig.getConnStr();
dbName = con.Database;
if (Type == 1)
{
sqlText = "backup database "+ dbName +" to disk ='" + FilePath + "' with init";
}
else if (Type == 2)
{
if (ExistsDB(dbName))
{
killProc(dbName);
}
sqlText = "restore database "+dbName+" from disk = '" + FilePath + "'";
}
try
{
_Oper.ExecSql(sqlText);
ReturnData = true;
}
catch(SqlException sqlEx) {
throw (sqlEx);
}
return ReturnData;
}
}
/// <summary>
/// 判断数据库是否存在
/// </summary>
/// <param name="dbName">数据库名称</param>
/// <returns></returns>
private bool ExistsDB(string dbName)
{
bool exist = false;
string sqlText = "if not exists(select * from master..sysdatabases where name='" + dbName + "') select getdate() else select null";
DataTable dt;
_Oper.ConnectionString = WebConfig.getMasterConn();
_Oper.ExecSql(sqlText, out dt);
if (dt.Rows[0][0]== Convert.DBNull)
{
exist = true;
}
return exist;
}
/// <summary>
/// 杀掉数据库进程
/// </summary>
/// <param name="dbName">要杀掉进程的数据库名</param>
private void killProc(string dbName)
{
_Oper.AddParameter("@dbname", dbName);
try
{
_Oper.ExecProc("killspid");//存储过程很多地方都有,到网上找
}
catch (SqlException ex)
{
throw new Exception(ex.Message);
}
}
public class DatabaseOper
{
public DataOperateBase _Oper;//数据库操作类对象
public DatabaseOper()
{
_Oper = new SqlDataOperate();
//WebConfig 为定义类
_Oper.ConnectionString = WebConfig.getMasterConn();//初始化连接
}
/// <summary>
/// 备份/恢复
/// </summary>
/// <param name="Type">操作类型,1为备份,2为恢复</param>
/// <param name="FilePath">路径,如D:\Bak\Bak.bak</param>
/// <returns></returns>
protected bool DatabaseBak(int Type, string FilePath)
{
bool ReturnData = false;
string dbName;
string sqlText = string.Empty;
SqlConnection con = new SqlConnection();
con.ConnectionString = WebConfig.getConnStr();
dbName = con.Database;
if (Type == 1)
{
sqlText = "backup database "+ dbName +" to disk ='" + FilePath + "' with init";
}
else if (Type == 2)
{
if (ExistsDB(dbName))
{
killProc(dbName);
}
sqlText = "restore database "+dbName+" from disk = '" + FilePath + "'";
}
try
{
_Oper.ExecSql(sqlText);
ReturnData = true;
}
catch(SqlException sqlEx) {
throw (sqlEx);
}
return ReturnData;
}
}
/// <summary>
/// 判断数据库是否存在
/// </summary>
/// <param name="dbName">数据库名称</param>
/// <returns></returns>
private bool ExistsDB(string dbName)
{
bool exist = false;
string sqlText = "if not exists(select * from master..sysdatabases where name='" + dbName + "') select getdate() else select null";
DataTable dt;
_Oper.ConnectionString = WebConfig.getMasterConn();
_Oper.ExecSql(sqlText, out dt);
if (dt.Rows[0][0]== Convert.DBNull)
{
exist = true;
}
return exist;
}
/// <summary>
/// 杀掉数据库进程
/// </summary>
/// <param name="dbName">要杀掉进程的数据库名</param>
private void killProc(string dbName)
{
_Oper.AddParameter("@dbname", dbName);
try
{
_Oper.ExecProc("killspid");//存储过程很多地方都有,到网上找
}
catch (SqlException ex)
{
throw new Exception(ex.Message);
}
}