主题:[讨论].net实现数据库备份和恢复的问题
public static string DbRestore(string dbName,string backupFile)
{
SQLDMO.Restore oRestore = new SQLDMO.RestoreClass();
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
try
{
oSQLServer.LoginSecure = false;
oSQLServer.Connect("127.0.0.1", "sa", "sa");
oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
oRestore.Database = dbName;
oRestore.Files = @"d:\\aaa\\"+backupFile;
oRestore.FileNumber = 1;
oRestore.ReplaceDatabase = true;
oRestore.SQLRestore(oSQLServer);
return "数据库"+dbName+"已经成功恢复!";
}
catch(Exception ex)
{
throw new Exception("数据库恢复失败: "+ex.Message );
}
finally
{
oSQLServer.DisConnect();
}
}
private void Button2_Click(object sender, System.EventArgs e)
{
string conStr = "server=.;database=master;user id=sa;password=sa";
SqlConnection con = new SqlConnection(conStr);
string cmdText = String.Format("EXEC sp_KillThread @dbname='{0}'", TextBox1.Text);
SqlCommand com = new SqlCommand(cmdText, con);
try
{
con.Open();
com.ExecuteNonQuery();
con.Close();
//恢复数据库
string ret = DBOperater.DbRestore(TextBox1.Text, TextBox2.Text);
this.Label1.Text = ret;
}
catch (Exception ex)
{
con.Close();
this.Label1.Text = ex.Message;
}
}
这是我的恢复数据库的代码``为什么报这个错误:
数据库恢复失败: [Microsoft][ODBC SQL Server Driver][SQL Server]无法打开备份设备 'd:\\aaa\\test'。设备出现错误或设备脱机。详细信息请参阅 SQL Server 错误日志。 [Microsoft][ODBC SQL Server Driver][SQL Server]RESTORE DATABASE 操作异常终止。
谁帮我解决下!!!
!!!!!!谢谢!!!!!!!!!!!11111
{
SQLDMO.Restore oRestore = new SQLDMO.RestoreClass();
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
try
{
oSQLServer.LoginSecure = false;
oSQLServer.Connect("127.0.0.1", "sa", "sa");
oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
oRestore.Database = dbName;
oRestore.Files = @"d:\\aaa\\"+backupFile;
oRestore.FileNumber = 1;
oRestore.ReplaceDatabase = true;
oRestore.SQLRestore(oSQLServer);
return "数据库"+dbName+"已经成功恢复!";
}
catch(Exception ex)
{
throw new Exception("数据库恢复失败: "+ex.Message );
}
finally
{
oSQLServer.DisConnect();
}
}
private void Button2_Click(object sender, System.EventArgs e)
{
string conStr = "server=.;database=master;user id=sa;password=sa";
SqlConnection con = new SqlConnection(conStr);
string cmdText = String.Format("EXEC sp_KillThread @dbname='{0}'", TextBox1.Text);
SqlCommand com = new SqlCommand(cmdText, con);
try
{
con.Open();
com.ExecuteNonQuery();
con.Close();
//恢复数据库
string ret = DBOperater.DbRestore(TextBox1.Text, TextBox2.Text);
this.Label1.Text = ret;
}
catch (Exception ex)
{
con.Close();
this.Label1.Text = ex.Message;
}
}
这是我的恢复数据库的代码``为什么报这个错误:
数据库恢复失败: [Microsoft][ODBC SQL Server Driver][SQL Server]无法打开备份设备 'd:\\aaa\\test'。设备出现错误或设备脱机。详细信息请参阅 SQL Server 错误日志。 [Microsoft][ODBC SQL Server Driver][SQL Server]RESTORE DATABASE 操作异常终止。
谁帮我解决下!!!
!!!!!!谢谢!!!!!!!!!!!11111