单个 SqlCommand 中的多个语句
我有一组 sql 脚本,我使用 C# 中的 SqlCommand 对象发送到 SQL Server。这些脚本创建存储过程,只要我创建这些过程,一切都会正常工作。如果我的脚本包含通常的“如果存在... drop XYZ; create procedure XYZ ...”块,我会收到一个错误,告诉我,create 必须是批处理中的第一个语句。分号和“GO”都不能用作分隔符。
有关如何使用单个 SqlCommand 执行此类脚本的任何提示吗?我期望能够将属性设置为“批量”或类似的东西,但我没有找到任何东西。
I have a set of sql scripts which I send to SQL server using a SqlCommand object in C#. These scripts create stored procedures and as long as I just create the procedures, everything works finde. If my scripts contain the usual "if exists ... drop XYZ; create procedure XYZ ..." block, I get an error which tells me, that create must be the first statement in a batch. Neither semicolon nor "GO" work as separator.
Any hint how to execute such a script using a single SqlCommand? I have expected to be able to set a property to "Batch" or something like that, but I did not found anything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
;应该可以正常工作,如 http://www.java2s 中所示.com/Code/CSharp/Database-ADO.net/ExecutemultipleSQLstatementsusingaSqlCommandobject.htm
但是您可以使用存储过程来完成此任务,这将使事情变得更容易。
; should work fine, as seen in http://www.java2s.com/Code/CSharp/Database-ADO.net/ExecutemultipleSQLstatementsusingaSqlCommandobject.htm
But you could use a stored procedure for this task which would make things easier.
如果你想提交一个包含批次的大型sql脚本,你应该使用SMO。这使您可以更好地控制脚本如何执行其操作。并且不依赖于自定义 sql 脚本粉碎逻辑(我需要解决这个问题吗?)
http://msdn.microsoft.com/en-us/library/ms212724.aspx
If you want to submit a large sql script with batches in it, you should use the SMO. This gives you a lot more control over how the scripts do their thing. And doesn't rely on custom sql script shredding logic (do I need to get into the issues with this?)
http://msdn.microsoft.com/en-us/library/ms212724.aspx
冲突的语句必须用批处理分隔符分隔(默认 GO - 你说它不起作用),或者,如果可能的话,从以不同顺序执行的程序逻辑的角度来看。但是,在大多数情况下,重组语句的顺序是不可能的,因此您必须在不同批次中分离,因此我建议在不同批次中运行它们。
The conflicting statements must either be separated by a batch separator (default GO - which you say doesn't work), or, if possible from the point of view of your program logic executed in a different order. However, in most case restructuring of the order of statements will not be possible so that you have to resort to the separation in different batches so I would suggest running them in different batches.