在 SQL Server 中使用 OLE DB 运行多个命令或 sql 脚本

发布于 2024-09-07 08:47:11 字数 141 浏览 3 评论 0原文

是否可以使用 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 技术交流群。

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

发布评论

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

评论(3

辞旧 2024-09-14 08:47:11

分隔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.

伏妖词 2024-09-14 08:47:11

马丁 - 我也遇到过同样的问题。我假设当您加载包含一个或多个“go”的脚本时,您会收到错误吗?

我过去曾使用 SQL DMO 来运行其中包含 GO 的脚本。这确实需要在目标计算机上安装 SQLDMO。

另一种选择是使用 .NET“String.Split(“GO”)”函数,并循环生成的字符串数组,逐个执行它们。

像这样:

StreamReader file = File.OpenText("c:\\MyScript.sql");
SqlConnection conn = new SqlConnection("My connection string here...");
string fullCommand = file.ReadToEnd();
string[] splitOptions = new string[] {"go", "GO", "gO", "Go" };
foreach (string individualCommand in fullCommand.Split(splitOptions, StringSplitOptions.RemoveEmptyEntries))
{
    SqlCommand comm = new SqlCommand(individualCommand, conn);
    comm.ExecuteNonQuery();
}

免责声明:我没有测试上面的代码,但它应该让您了解需要什么:-)

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:

StreamReader file = File.OpenText("c:\\MyScript.sql");
SqlConnection conn = new SqlConnection("My connection string here...");
string fullCommand = file.ReadToEnd();
string[] splitOptions = new string[] {"go", "GO", "gO", "Go" };
foreach (string individualCommand in fullCommand.Split(splitOptions, StringSplitOptions.RemoveEmptyEntries))
{
    SqlCommand comm = new SqlCommand(individualCommand, conn);
    comm.ExecuteNonQuery();
}

Disclaimer: I have not tested the above code, but it should give you an idea of what is required :-)

意中人 2024-09-14 08:47:11

我已经发送了多个语句,如 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.

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