我已经定义了所有的东西,这段代码是我的代码的一部分。
if (Sql_Conn == null)
Sql_Conn = new SqlConnection(Str_Con);
// str_con is my connection string
if (Sql_Conn.State != ConnectionState.Open)
Sql_Conn.Open();
Data_Table = new DataTable();
DataA_dapter = new SqlDataAdapter();
Sql_Cmd = new SqlCommand();
Sql_Cmd.Connection = Sql_Conn; //
string sql = "RESTORE DATABASE [" + str_directory + "] From DISK = " +
"'" + strFileName + "' ; ";
// str_directory is the source of my database as DB.MDF
// srtFileName is the directory of DB.bak
Sql_Cmd.CommandType = CommandType.Text;
Sql_Cmd.CommandText = sql;
try
{
Sql_Cmd.ExecuteNonQuery();
}
catch (SqlException Error_Exception)
{
MessageBox.Show("Error");
}当我在SQL Sserver中使用string sql和新查询时,我没有问题,我的数据库成功地恢复了,但是当我在c#中使用这段代码时,我看到了这个错误。
错误:{System.Data.SqlClient.SqlException: RESTORE不能处理数据库'E:/DB.mdf‘,因为这个会话正在使用它。建议在执行此操作时使用主数据库。还原数据库正在异常终止。
我想恢复我的数据库。我必须在我的第一个代码打开连接,当我想恢复我的数据库时,我会看到异常。
现在我如何恢复我的数据库?请帮帮我。
发布于 2011-08-06 18:05:40
检查连接字符串。如果它包含初始目录设置,则将其设置为主目录。
发布于 2011-08-06 18:04:07
无法还原连接到的确切数据库。正如错误消息所提到的,您应该使用master数据库。
在连接字符串中使用database=master。
发布于 2011-08-06 18:05:22
您需要连接到与您试图还原的数据库不同的数据库。在连接字符串中设置不同的数据库(inital目录),连接,然后运行代码。
See how to restore sql-server database Through C# code用于解决相同的问题。
https://stackoverflow.com/questions/6968603
复制相似问题