无法在脱机数据库上调用 sp_detach_db
我可以在 SqlManager 中运行此命令来分离数据库
ALTER DATABASE mydb SET OFFLINE WITH ROLLBACK IMMEDIATE
GO
dbo.sp_detach_db @dbname = N'mydb',@keepfulltextindexfile = N'false'
当我使用相同的连接通过 ado.net 运行相同的commadn 失败并出现错误时:(
The database 'mydb' can not be opened because it is offline
错误是从德语翻译的。)
Ado.Net 代码是
SqlCommand cmdOffline = new SqlCommand(@"ALTER DATABASE mydb SET OFFLINE WITH ROLLBACK IMMEDIATE");
cmdOffline.Connection = prepareMasterDBConnection;
cmdOffline.ExecuteNonQuery();
SqlCommand cmdDetach = new SqlCommand(@"dbo.sp_detach_db @dbname = N'mydb',@keepfulltextindexfile = N'false'");
cmdDetach.Connection = prepareMasterDBConnection;
cmdDetach.ExecuteNonQuery();
连接设置为主 -数据库并打开。第一个命令成功执行。
从 ado 和 sql-manager 调用代码时有什么区别?
I can run this command in SqlManager to detach the db
ALTER DATABASE mydb SET OFFLINE WITH ROLLBACK IMMEDIATE
GO
dbo.sp_detach_db @dbname = N'mydb',@keepfulltextindexfile = N'false'
When I use the same connection running the same commadn via ado.net fails with error:
The database 'mydb' can not be opened because it is offline
(Error is translated from german.)
The Ado.Net code is
SqlCommand cmdOffline = new SqlCommand(@"ALTER DATABASE mydb SET OFFLINE WITH ROLLBACK IMMEDIATE");
cmdOffline.Connection = prepareMasterDBConnection;
cmdOffline.ExecuteNonQuery();
SqlCommand cmdDetach = new SqlCommand(@"dbo.sp_detach_db @dbname = N'mydb',@keepfulltextindexfile = N'false'");
cmdDetach.Connection = prepareMasterDBConnection;
cmdDetach.ExecuteNonQuery();
The connection is set to master - DB and open. The first commadn exceutes sucessfully.
What is the difference here when calling the code from ado and from sql-manager?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的目标是在删除它时避免连接冲突,而不是在分离之前将其设置为脱机,我会使用命令
ALTER DATABASE mydb SET SINGLE_USER WITH ROLLBACK IMMEDIATE
而不是将其设置为脱机(并将其反转)与ALTER DATABASE mydb SET MULTI_USER
)。If your goal is to avoid conflicting connections while dropping it, rather than setting it offline before detaching, I would use the command,
ALTER DATABASE mydb SET SINGLE_USER WITH ROLLBACK IMMEDIATE
instead of setting it offline (and reverse it withALTER DATABASE mydb SET MULTI_USER
).Detach 在分离之前需要做一些事情。就像 s_detach 所说(我的粗体)
离线的时候就不能这样了
Detach needs to do some stuff before it detaches. Like s_detach says (my bold)
When it's offline, you can't do that...