在做数据库恢复时,抛出异常
如图:代码如下:
public void DbRestore(string fileName)
{
SQLDMO.Restore oRestore = new SQLDMO.RestoreClass();
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
try
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=.;Initial Catalog=master;User ID=sa;pwd =123456;pooling=false";
conn.Open();
//查找当前名为PUBS的数据库使用进程
string sql = "select spid from sysprocesses,sysdatabases where sysprocesses.dbid=sysdatabases.dbid and sysdatabases.Name = 'VisionMaster'";
SqlCommand cmd1 = new SqlCommand(sql, conn);
SqlDataReader dr;
ArrayList list = new ArrayList();
try
{
dr = cmd1.ExecuteReader();
while (dr.Read())
{
list.Add(dr.GetInt16(0));
}
dr.Close();
}
catch (SqlException)
{
conn.Close();
}
for (int i = 0; i < list.Count; i++)
{
cmd1 = new SqlCommand(string.Format("KILL {0}", list[i].ToString()), conn);
cmd1.ExecuteNonQuery();
}
oSQLServer.LoginSecure = false;
oSQLServer.Connect("localhost", "sa", "123456");
oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
oRestore.Database = "VisionMaster";
string filePath = Server.MapPath("~\\Data\\" + fileName);
oRestore.Files = filePath;
oRestore.FileNumber = 1;
oRestore.ReplaceDatabase = true;
oRestore.SQLRestore(oSQLServer);
}
catch(Exception ex)
{
throw ex;
}
finally
{
oSQLServer.DisConnect();
Response.Write("<script>window.close();</script>");
ui.AlertRedirect(this, "恢复成功,请重新数据库连接服务", "../Login.aspx");
}
}
如图:代码如下:
public void DbRestore(string fileName)
{
SQLDMO.Restore oRestore = new SQLDMO.RestoreClass();
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
try
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=.;Initial Catalog=master;User ID=sa;pwd =123456;pooling=false";
conn.Open();
//查找当前名为PUBS的数据库使用进程
string sql = "select spid from sysprocesses,sysdatabases where sysprocesses.dbid=sysdatabases.dbid and sysdatabases.Name = 'VisionMaster'";
SqlCommand cmd1 = new SqlCommand(sql, conn);
SqlDataReader dr;
ArrayList list = new ArrayList();
try
{
dr = cmd1.ExecuteReader();
while (dr.Read())
{
list.Add(dr.GetInt16(0));
}
dr.Close();
}
catch (SqlException)
{
conn.Close();
}
for (int i = 0; i < list.Count; i++)
{
cmd1 = new SqlCommand(string.Format("KILL {0}", list[i].ToString()), conn);
cmd1.ExecuteNonQuery();
}
oSQLServer.LoginSecure = false;
oSQLServer.Connect("localhost", "sa", "123456");
oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
oRestore.Database = "VisionMaster";
string filePath = Server.MapPath("~\\Data\\" + fileName);
oRestore.Files = filePath;
oRestore.FileNumber = 1;
oRestore.ReplaceDatabase = true;
oRestore.SQLRestore(oSQLServer);
}
catch(Exception ex)
{
throw ex;
}
finally
{
oSQLServer.DisConnect();
Response.Write("<script>window.close();</script>");
ui.AlertRedirect(this, "恢复成功,请重新数据库连接服务", "../Login.aspx");
}
}
执行到这一步就出错了,无法运行下去,
如果将其注释掉,他会跳转至登录页面
ui.AlertRedirect(this, "恢复成功,请重新数据库连接服务", "../Login.aspx");
重新登录,则会在登录页面抛出异常,异常如上图
执行到这里就执行不下去了
{
SQLDMO.Restore oRestore = new SQLDMO.RestoreClass();
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
try
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=.;Initial Catalog=master;User ID=sa;pwd =123456;pooling=false";
conn.Open();
//查找当前名为PUBS的数据库使用进程
string sql = "select spid from sysprocesses,sysdatabases where sysprocesses.dbid=sysdatabases.dbid and sysdatabases.Name = 'VisionMaster'";
SqlCommand cmd1 = new SqlCommand(sql, conn);
SqlDataReader dr;
ArrayList list = new ArrayList();
try
{
dr = cmd1.ExecuteReader();
while (dr.Read())
{
list.Add(dr.GetInt16(0));
}
dr.Close();
}
catch (SqlException)
{
conn.Close();
}
for (int i = 0; i < list.Count; i++)
{
cmd1 = new SqlCommand(string.Format("KILL {0}", list[i].ToString()), conn);
cmd1.ExecuteNonQuery();
}
oSQLServer.LoginSecure = false;
oSQLServer.Connect("localhost", "sa", "123456");
oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
oRestore.Database = "VisionMaster";
string filePath = Server.MapPath("~\\Data\\" + fileName);
oRestore.Files = filePath;
oRestore.FileNumber = 1;
oRestore.ReplaceDatabase = true;
oRestore.SQLRestore(oSQLServer);
}
catch(Exception ex)
{
throw ex;
}
finally
{
oSQLServer.DisConnect();
Response.Write("<script>window.close();</script>");
ui.AlertRedirect(this, "恢复成功,请重新数据库连接服务", "../Login.aspx");
}
}
应该对的吧
跳转到登录页面,之后便会提示在向服务器发送请求时发生传输级错误。 (provider: TCP Provider, error: 0 - 远程主机强迫关闭了一个现有的连接。)
无法登录
现在我要怎么改呢?