SQLDB备份还原
扫描二维码
随时随地手机看文章
SQLDB备份
private void Button2_Click(object sender, System.EventArgs e)
{
string Dbserver = System.Configuration.ConfigurationSettings.AppSettings["Dbserver"];
string DbUserId = System.Configuration.ConfigurationSettings.AppSettings ["masterDbUserId"];
string DbUserPSW = System.Configuration.ConfigurationSettings.AppSettings["masterDbUserPSW"];
string baseName = System.Configuration.ConfigurationSettings.AppSettings ["Dbase"];
string path = Server.MapPath(System.Configuration.ConfigurationSettings.AppSettings["BackUpDB"]) + baseName + ".bak";
SQLDMO.Restore oRestore = new SQLDMO.RestoreClass();
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
try
{
oSQLServer.LoginSecure = false;
oSQLServer.Connect(Dbserver, DbUserId, DbUserPSW);
oRestore.Action =
SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database ;
oRestore.Database = baseName;
oRestore.Files = path;
oRestore.FileNumber = 1;
oRestore.ReplaceDatabase = true;
oRestore.SQLRestore(oSQLServer);
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
finally
{
oSQLServer.DisConnect();
CommonClass.MsgBox("数据库还原成功");
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
string Dbserver = System.Configuration.ConfigurationSettings.AppSettings["Dbserver"];
string DbUserId = System.Configuration.ConfigurationSettings.AppSettings ["masterDbUserId"];
string DbUserPSW = System.Configuration.ConfigurationSettings.AppSettings["masterDbUserPSW"];
string baseName = System.Configuration.ConfigurationSettings.AppSettings["Dbase"];
string path = Server.MapPath(System.Configuration.ConfigurationSettings.AppSettings["BackUpDB"]) + baseName + ".bak";
SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass ();
try
{
oSQLServer.LoginSecure = false;
oSQLServer.Connect(Dbserver, DbUserId, DbUserPSW);
oBackup.Action =
SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
oBackup.Database = baseName;
oBackup.Files = path;
oBackup.BackupSetName = baseName;
oBackup.BackupSetDescription = "数据库备份";
oBackup.Initialize = true;
oBackup.SQLBackup(oSQLServer);
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
finally
{
oSQLServer.DisConnect();
CommonClass.MsgBox("数据库备份成功!");
}
}
//webconfig