无法在脱机数据库上调用 sp_detach_db

发布于 2024-08-23 03:58:53 字数 917 浏览 5 评论 0原文

我可以在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南汐寒笙箫 2024-08-30 03:58:53

如果您的目标是在删除它时避免连接冲突,而不是在分离之前将其设置为脱机,我会使用命令 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 with ALTER DATABASE mydb SET MULTI_USER).

人事已非 2024-08-30 03:58:53

Detach 在分离之前需要做一些事情。就像 s_detach 所说(我的粗体)

@skipchecks = '跳过检查'

指定是跳过还是运行
更新统计数据。跳过检查是一个
nvarchar(10) 值,具有默认值
值为 NULL。跳过更新
统计数据,指定 true。到
显式运行 UPDATE STATISTICS,
指定 false。

默认情况下,更新统计信息是
执行
以更新有关的信息
表和索引中的数据
SQL Server 2005 数据库引擎。
执行 UPDATE STATISTICS 很有用
对于要移动到的数据库
只读媒体。

离线的时候就不能这样了

Detach needs to do some stuff before it detaches. Like s_detach says (my bold)

@skipchecks = 'skipchecks'

Specifies whether to skip or run
UPDATE STATISTIC. skipchecks is a
nvarchar(10) value, with a default
value of NULL. To skip UPDATE
STATISTICS, specify true. To
explicitly run UPDATE STATISTICS,
specify false.

By default, UPDATE STATISTICS is
performed
to update information about
the data in the tables and indexes in
the SQL Server 2005 Database Engine.
Performing UPDATE STATISTICS is useful
for databases that are to be moved to
read-only media.

When it's offline, you can't do that...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文