首先附上代码,代码没问题。
本地备份和恢复都没问题,本地环境 Windows Server 2003 + Microsoft Visual Studio 2005 + SQL Server 2000
放到服务器上报错。错误如下
Retrieving the COM class factory for component with CLSID {10021F00-E260-11CF-AE68-00AA004A34D5}
failed the due to following error:80070005
开始以为是没上传 Interop.SQLDMO.dll 这个程序集到Bin目录下,发现其实已存在。
后来想到权限问题,给了 Database_Bak 这个文件夹的ASP.NET和EveryOne的完全控制权限,发现还报这个错。
请各位大虾指教。
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Reflection;
using System.IO;
using System.Data.SqlClient;
public partial class Admin_Admin_DataBase_Admin_Admin_DBBackup : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.QueryString["Action"] == null)
{
Panel2.Visible = true;
Panel1.Visible = false;
}
else
{
Panel2.Visible = false;
Panel1.Visible=true;
}
}
}
/* 备份数据库 */
protected void Button1_Click(object sender, EventArgs e)
{
BackupDB();//备份数据库
}
/* 恢复数据库 */
protected void Button2_Click(object sender, EventArgs e)
{
RestoreDB();//恢复数据库
}
/* 备份数据库*/
public bool BackupDB()
{
string StrPath = CreatePath();//创建路径
//Response.Write(StrPath);
//Response.End();
SQLDMO.Backup ObjBackup = new SQLDMO.BackupClass();
SQLDMO.SQLServer ObjSQLServer = new SQLDMO.SQLServerClass();
try
{
ObjSQLServer.LoginSecure = false;
ObjSQLServer.Connect("127.0.0.1", "sa", "123456");
ObjBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
ObjBackup.Database = "BlogStar";
ObjBackup.Files = StrPath;
ObjBackup.BackupSetName = "BlogStar";
ObjBackup.BackupSetDescription = "数据库备份";
ObjBackup.Initialize = true;
ObjBackup.SQLBackup(ObjSQLServer);
LabNote.Text = "备份成功";
return true;
}
catch (Exception ErrorMessage)
{
LabNote.Text = ErrorMessage.ToString();
throw ErrorMessage;
return false;
}
finally
{
ObjSQLServer.DisConnect();
}
}
/* 创建路径 */
public string CreatePath()
{
string StrFielName=TexDBName.Text.ToString();
//string CurrDate = System.DateTime.Now.ToString();
//CurrDate = CurrDate.Replace("-", "");
//CurrDate = CurrDate.Replace(":", "");
//CurrDate = CurrDate.Replace(" ", "");
//CurrDate = CurrDate.Substring(0, 12);
string Path = Server.MapPath("../../Database_Bak\\");
Path += "BlogStar";
Path += "_DB_";
Path += StrFielName;
Path += ".Bak";
return Path;
}
/* 恢复数据库*/
public string RestoreDB()
{
if (KillProcedure() != true)//杀死当前库的所有进程
{
Response.Write("qqqq");
//Response.End();
return "操作失败";
}
else
{
string Path = Server.MapPath("../../Database_Bak\\BlogStar_DB_");
Path += TexDBName2.Text.ToString() + ".Bak";
SQLDMO.Restore ObjRestore = new SQLDMO.RestoreClass();
SQLDMO.SQLServer ObjSQLServer = new SQLDMO.SQLServerClass();
try
{
ObjSQLServer.LoginSecure = false;
ObjSQLServer.Connect("127.0.0.1", "sa", "123456");
ObjRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
ObjRestore.Database = "BlogStar";
ObjRestore.Files = Path;
ObjRestore.FileNumber = 1;
ObjRestore.ReplaceDatabase = true;
ObjRestore.SQLRestore(ObjSQLServer);
LabNote2.Text = "恢复成功";
return "Ok";
}
catch (Exception ErrorMessage)
{
LabNote2.Text = ErrorMessage.ToString();
throw ErrorMessage;
return "恢复数据库失败";
}
finally
{
ObjSQLServer.DisConnect();
}
}
}
/* 杀死当前库的所有进程 */
public bool KillProcedure()
{
SqlConnection Conn = new SqlConnection("server=.;uid=sa;pwd=123456;database=master");
SqlCommand Cmd = new SqlCommand("killspid", Conn);
Cmd.CommandType = CommandType.StoredProcedure;
Cmd.Parameters.Add("@dbname", "BlogStar");
try
{
Conn.Open();
Cmd.ExecuteNonQuery();
return true;
}
catch(Exception ex)
{
return false;
}
finally
{
Conn.Close();
SqlConnection.ClearAllPools();//清空连接池
}
}
}
在Windows 2003上需要给Network Service 账号授权;
ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.
如何解决的呢?