在单一的SqlCommand多个语句 [英] Multiple statements in single SqlCommand

查看:142
本文介绍了在单一的SqlCommand多个语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组我发送到SQL Server使用C#SqlCommand对象的SQL脚本。这些脚本创建存储过程只要我刚刚创建的程序,一切正常FINDE。如果我的脚本包含了通常的是否存在?降XYZ;创建过程XYZ ......块,我得到一个错误,告诉我​​,创造必须是批处理的第一条语句。无论是分号,也不是GO的工作作为分隔符。

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.

任何提示如何使用单一的SqlCommand执行这样一个剧本?我也希望能够设置一个属性,以批或类似的东西,但我没有发现任何东西。

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.

推荐答案

有冲突的语句必须被一个批处理分隔符(默认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.

这篇关于在单一的SqlCommand多个语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆