如果我们使用虚拟主机为网站空间,这时如果需要备份和还原msssql数据库是非常麻烦,如果在网站后台管理当中加入对msssql数据库的操纵,可以使我们对数据库的备份和还原操作很方便,只要一键便可生成。
C#(asp.net)备份mssql数据库代码
string path = CreatePath();
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 = path;
oBackup.BackupSetName = database;
oBackup.BackupSetDescription = "数据库备份";
oBackup.Initialize = true;
oBackup.SQLBackup(oSQLServer);
return true;
}
catch (Exception ex)
{
return false;
throw ex;
}
finally
{
oSQLServer.DisConnect();
}
C#(asp.net)还原mssql数据库代码
SQLDMO.Restore oRestore = new SQLDMO.RestoreClass();
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
oRestore.Action = 0;
//SQLDMO.RestoreSink_PercentCompleteEventHandler pceh = new SQLDMO.RestoreSink_PercentCompleteEventHandler(Step);
// oRestore.PercentComplete += pceh;
try
{
oSQLServer.Connect(server, uid, pwd);
SQLDMO.QueryResults qr = oSQLServer.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() == "CgRecord".ToUpper())
oSQLServer.KillProcess(lPID);
}
oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
oRestore.Database = database;
oRestore.Files = HttpContext.Current.Server.MapPath(DropDownList_path.SelectedValue.Trim());
oRestore.FileNumber = 1;
oRestore.ReplaceDatabase = true;
oRestore.SQLRestore(oSQLServer);
base.RegisterStartupScript("page", "window.location.href='DbBackup.aspx';", "" + gt.TranslateChineseToOther("还原成功", "zh-CN", Language) + "");
}
catch (Exception ex)
{
base.RegisterStartupScript("page", "window.location.href='DbBackup.aspx';", "" + gt.TranslateChineseToOther("恢复数据库失败,请关闭所有和该数据库连接的程序!", "zh-CN", Language) + "");
//throw ex;
}
finally
{
oSQLServer.DisConnect();
}
25亿企业网站管理系统具备此功能,大家可以一起探讨25亿企业网站管理系统。