使用SMO,仍然不行... ConnectionContext.ExecuteNonQuery(script) 无法理解“GO”
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“GO”不是 SQL 语言
它是 SSMS 等客户端工具使用的批次分隔符(不会将“GO”发送到数据库引擎)
SMO 不会像 SSMS 那样将脚本解析为批次,因此数据库引擎会抛出错误。
评论后:
“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)
or this correctly formatted?
由于查询文本格式错误而导致的错误。实在是太尴尬了。
现在解决了,谢谢!
Error caused by faulty formatting of query text. Quite embarrassing really.
Now solved, thanks!