使用SMO,仍然不行... ConnectionContext.ExecuteNonQuery(script) 无法理解“GO”

发布于 2024-08-08 21:24:00 字数 1331 浏览 3 评论 0原文

SQL Server 2008

使用所有正确的引用我敢说:

System.Data.SqlClient;
Microsoft.SqlServer.Management.Smo;
Microsoft.SqlServer.Management.Common;
Microsoft.SqlServer.Management.Sdk.Sfc;

所有编译都没有错误。

为了便于调试,我已将代码精简到几乎为零。

连接到服务器正常等等。

执行以下代码:

SqlConnection connection = new SqlConnection(sqlConnectionString);
Server server = new Server(new ServerConnection(connection));
server.ConnectionContext.ExecuteNonQuery(sqlDBQuery);

其中 sqlDBQuery 是一个字符串: USE [master] GO ALTER DATABASE [Cassiopeia] SET ANSI_NULL_DEFAULT OFF GO ALTER DATABASE [Cassiopeia] SET ANSI_NULLS OFF GO

但它没有不管“sqlDBQuery”是什么,我总是得到同样的错误,比如

GO 附近的语法不正确

我相信 SMO 会处理这个问题,当我查看我的 ConnectionContext 时,显示 BatchSeparator = "GO"

如果我​​删除 GO,那就可以了......可以这么说,但我真的需要知道为什么我的 SMO 不起作用。

我到处都看到它只是说“像这样使用 smo,你就没事了”。嗯...对我不起作用。

请参阅 Jon Galloway 的这篇文章以供参考: http://weblogs.asp.net/jgalloway/archive/2006/11/07/Handling-_2200_GO_2200_-Separators-in-SQL-Scripts-2D00-the-easy-way .aspx

问候

SQL Server 2008

Using all the correct references I dare say:

System.Data.SqlClient;
Microsoft.SqlServer.Management.Smo;
Microsoft.SqlServer.Management.Common;
Microsoft.SqlServer.Management.Sdk.Sfc;

All compiles with no errors.

I have stripped code down to almost zero for easy debugging.

Connecting to server alright and so on.

Excuting following code:

SqlConnection connection = new SqlConnection(sqlConnectionString);
Server server = new Server(new ServerConnection(connection));
server.ConnectionContext.ExecuteNonQuery(sqlDBQuery);

Where sqlDBQuery is a string: USE [master] GO ALTER DATABASE [Cassiopeia] SET ANSI_NULL_DEFAULT OFF GO ALTER DATABASE [Cassiopeia] SET ANSI_NULLS OFF GO

But it doesn't matter what "sqlDBQuery" is, I always get the same error, like

incorrect syntax near GO

.

I was in belief that SMO would take care of this, when I look at my ConnectionContext is says BatchSeparator = "GO"

If I remove GO it's a go... so to speak but I really need to know why my SMO doesn't work.

Everywhere I look it just says "use smo like this and you're off fine". Well... doesn't work for me.

See this post by Jon Galloway for reference:
http://weblogs.asp.net/jgalloway/archive/2006/11/07/Handling-_2200_GO_2200_-Separators-in-SQL-Scripts-2D00-the-easy-way.aspx

Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

烈酒灼喉 2024-08-15 21:24:00

“GO”不是 SQL 语言

它是 SSMS 等客户端工具使用的批次分隔符(不会将“GO”发送到数据库引擎)

SMO 不会像 SSMS 那样将脚本解析为批次,因此数据库引擎会抛出错误。

评论后:

“GO”本身必须单独一行,

您的脚本实际上是这样的(从我“编辑”问题时复制/粘贴)

USE [master]  GO  ALTER DATABASE [Cassiopeia] SET ANSI_NULL_DEFAULT OFF  GO  ALTER DATABASE [Cassiopeia] SET ANSI_NULLS OFF  GO

还是格式正确?

USE [master]
GO
ALTER DATABASE [Cassiopeia] SET ANSI_NULL_DEFAULT OFF
GO
ALTER DATABASE [Cassiopeia] SET ANSI_NULLS OFF
GO

"GO" is not SQL language

It's a batch separator used by client tools like SSMS (which does not send "GO" to the database engine)

SMO does not parse the script into batches like SSMS would do, so the database engine throws an error.

After comment:

"GO" must be on a separate line by itself

Is your script literally this (copy/paste from when I "edit" the question)

USE [master]  GO  ALTER DATABASE [Cassiopeia] SET ANSI_NULL_DEFAULT OFF  GO  ALTER DATABASE [Cassiopeia] SET ANSI_NULLS OFF  GO

or this correctly formatted?

USE [master]
GO
ALTER DATABASE [Cassiopeia] SET ANSI_NULL_DEFAULT OFF
GO
ALTER DATABASE [Cassiopeia] SET ANSI_NULLS OFF
GO
情丝乱 2024-08-15 21:24:00

由于查询文本格式错误而导致的错误。实在是太尴尬了。

现在解决了,谢谢!

Error caused by faulty formatting of query text. Quite embarrassing really.

Now solved, thanks!

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