/// <summary>
/// 数据库的备份与恢复
/// </summary>
public class BackupAndRestoreDb
{
/// <summary>
/// 备份数据库
/// </summary>
/// <param name="backupPath"></param>
public void BackDb(string backupPath)
{
if (File.Exists(backupPath))
{
File.Delete(backupPath);
}
string strSql = "backup database AddressList to disk=@backupPath";
SqlParameter[] parameters = {new SqlParameter("@backupPath", SqlDbType.NVarChar, 200)};
parameters[0].Value = backupPath;
SqlDbHelper.ExecuteNonQuery(strSql, CommandType.Text, parameters);
}
/// <summary>
/// 恢复数据库
/// </summary>
/// <param name="restorePath"></param>
public void RestoreDb(string restorePath)
{
string strSql = "Alter Database AddressList Set OffLine with Rollback immediate;" +
"use master;" +
"restore database AddressList from disk=@restorePath With Replace;" +
"Alter Database AddressLust Set OnLine With rollback Immediate";
SqlParameter[] parameters = {new SqlParameter("@restorePath", SqlDbType.NVarChar, 200)};
parameters[0].Value = restorePath;
SqlDbHelper.ExecuteNonQuery(strSql, CommandType.Text, parameters);
}
}