我已经定义了每一件事情,这代码是我的代码问题在还原数据库在C#
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:恢复无法 过程数据库“E:/DB.mdf”,因为它是在使用了本会话。建议在执行此 操作时使用主数据库 。 RESTORE DATABASE正在异常终止。
我想恢复我的数据库。我必须在我的第一个代码打开连接,当我想恢复我的数据库时,我看到异常。
现在我该如何恢复我的数据库?请帮帮我。
[如何通过C#代码恢复SQL Server数据库](http://stackoverflow.com/questions/4218960/how-to-restore-sql-server-database-through-c-code) –