/// <summary>
/// SQL数据库备份
/// </summary>
/// <param name="ServerIP">SQL服务器IP或(Localhost)</param>
/// <param name="LoginName">数据库登录名</param>
/// <param name="LoginPass">数据库登录密码</param>
/// <param name="DBName">数据库名</param>
/// <param name="BackPath">备份到的路径</param>
public static void SQLBACK(string ServerIP,string LoginName,string LoginPass,string DBName,string BackPath)
{
SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
try
{
oSQLServer.LoginSecure = false;
oSQLServer.Connect(ServerIP, LoginName, LoginPass);
oBackup.Database = DBName;
oBackup.Files = BackPath;
oBackup.BackupSetName = DBName;
oBackup.BackupSetDescription = "数据库备份";
oBackup.Initialize = true;
oBackup.SQLBackup(oSQLServer);
}
catch (Exception e)
{
throw new Exception(e.ToString());
}
finally
{
oSQLServer.DisConnect();
}
}
#endregion
#region SQL恢复数据库
/// <summary>
/// SQL恢复数据库
/// </summary>
/// <param name="ServerIP">SQL服务器IP或(Localhost)</param>
/// <param name="LoginName">数据库登录名</param>
/// <param name="LoginPass">数据库登录密码</param>
/// <param name="DBName">要还原的数据库名</param>
/// <param name="BackPath">数据库备份的路径</param>
public static void SQLDbRestore(string ServerIP,string LoginName,string LoginPass,string DBName,string BackPath)
{
SQLDMO.Restore orestore = new SQLDMO.RestoreClass();
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
try
{
oSQLServer.LoginSecure = false;
oSQLServer.Connect(ServerIP, LoginName, LoginPass);
orestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
orestore.Database = DBName;
orestore.Files = BackPath;
orestore.FileNumber = 1;
orestore.ReplaceDatabase = true;
orestore.SQLRestore(oSQLServer);
}
catch (Exception e)
{
throw new Exception(e.ToString());
}
finally
{
oSQLServer.DisConnect();
}
}
#endregion