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”,因为该会话正在使用它。它 建议执行此操作时使用 master 数据库 手术。 RESTORE DATABASE 异常终止。
我想恢复我的数据库。我必须在第一个代码处打开连接,当我想恢复数据库时,我看到异常。
现在我该如何恢复我的数据库?请帮我。
I define every thing Already and this code is a part of my code
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");
}
When I use string sql
in SQL Sserver with new query I don't have problem and my database restores successfully but when I use this code with c# I see this error
Error : {System.Data.SqlClient.SqlException: RESTORE cannot
process database 'E:/DB.mdf' because it is in use by this session. It
is recommended that the master database be used when performing this
operation. RESTORE DATABASE is terminating abnormally.
I want to restore my database. I have to open connection at the first of my codes and when I want to restore my database I see Exception.
Now how can I restore my database? Please Help Me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您无法恢复已连接的确切数据库。正如错误消息所提到的,您应该使用
master
数据库。在连接字符串中使用
database=master
。You can't restore the exact database that you have connected to. As the error message mentions, you should use the
master
database instead.In your connections string use
database=master
.您需要连接到与尝试恢复的数据库不同的数据库。在连接字符串中设置不同的数据库(初始目录),连接,然后运行代码。
了解如何通过 C# 代码恢复 sql-server 数据库 对于同样的问题。
You need to be connected to a different database than the one you are trying to restore. Set a different database (inital catalog) in you connection string, connect, then run the code.
See how to restore sql-server database Through C# code for the same problem.
问题是您的连接字符串正在初始化,初始目录(或数据库)设置为您要恢复的数据库。这会导致连接处于活动状态(您可以通过执行 sp_who2 系统存储过程来检查这一点),从而禁止您进行恢复。您应该将连接字符串中的初始目录(或数据库)值设置为 master 数据库。这将使您的目标数据库摆脱连接,并允许您执行恢复过程。
The problem is that you connection string is being initialized with Initial Catalog (or Database) set to the database that you want to restore. This causes a connection to be active (you can check this by executing sp_who2 system stored procedure) thus prohibiting you to do restore. You should set your Initial Catalog (or Database) value in a connection string to master database. This should leave your target database free from connections and will allow you to do the restore process.
检查连接字符串。如果它包含初始目录设置,请将其设置为“主目录”。
Check the connection string. If it contains an Initial Catalog setting set it to Master.