在 SQL Server 中使用 OLE DB 运行多个命令或 sql 脚本
是否可以使用 OLE DB 运行多个命令或 sql 脚本?例如运行 sql 脚本来创建数据库及其结构(表、索引、存储过程...)。
当我使用 CCommand 类时,我只能运行一个命令。如何使用多个命令运行 SQL 脚本?
谢谢, 马丁
It is possible to run multiple commands or sql script using OLE DB? For example to run sql script to create database and its structure (tables, indexes, stored procedures...).
When I use CCommand class, I can run only one command. How can I run SQL script with multiple commands?
Thanks,
Martin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
分隔TSQL语句的GO命令不是SQL命令,而是仅前端识别的语句。
因此,在将结果传递给命令对象之前,您需要在“\nGO”处将脚本显式拆分为多个命令。
The GO command which separates TSQL statements is NOT a SQL command, but a statement recognized by the front end only.
Therefore you need to explicitly split your script into several commands at "\nGO" before passing the result to the command object.
马丁 - 我也遇到过同样的问题。我假设当您加载包含一个或多个“go”的脚本时,您会收到错误吗?
我过去曾使用 SQL DMO 来运行其中包含 GO 的脚本。这确实需要在目标计算机上安装 SQLDMO。
另一种选择是使用 .NET“String.Split(“GO”)”函数,并循环生成的字符串数组,逐个执行它们。
像这样:
免责声明:我没有测试上面的代码,但它应该让您了解需要什么:-)
Martin - I have had this same issue. I assume that when you load a script with one or more "go" in it you get an error?
I have used SQL DMO in the past to run scripts with GO in them. That does require that SQLDMO be installed on your target computer.
Another option is to use the .NET "String.Split("GO")" function, and loop the resulting array of strings, exexuting them one by one.
Like this:
Disclaimer: I have not tested the above code, but it should give you an idea of what is required :-)
我已经发送了多个语句,如 INSERT INTO;INSERT INTO (在 SQL Server 中不需要分号。
某些语句(如 CREATE FUNCTION)必须是“批处理”中的第一个语句。在 SSMS 和 sqlcmd 中,您有一个 GO 分隔符 客户端工具,这不是 OLEDB 功能,因此您必须以串行或并行的单独调用方式单独发送它们(取决于您的操作是否可以同时运行)。
,这是一个用于分隔批次的 远离 CCommand,我想你可以启动 sqlcmd,因为它支持 GO 批处理分隔符。
I've sent multiple statements like INSERT INTO;INSERT INTO (semicolons are not required in SQL Server.
Some statements (like CREATE FUNCTION) have to be the first statement in a "Batch". In SSMS and sqlcmd, you have a GO separator, which is a client-side tool for separating batches. This is not an OLEDB feature, so you would have to send them separately - in a separate invocation in series or parallel (depending on whether your operations can run simultaneously).
If you can get away from CCommand, I guess you could launch sqlcmd, since it supports GO batch separators.