如何使用SqlConnection使用注释和GO语句执行SQL?

And*_*ott 11 c# sql database

我似乎无法执行使用DbCommand对象创建数据库的SQL.我究竟做错了什么?这是我的代码:

DbConnection connection; // initialized and opened elsewhere
DbCommand cmd = connection.CreateCommand();
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
Run Code Online (Sandbox Code Playgroud)

这是错误:

查询语法无效.,在术语'/',第1行,第2列附近.说明:在执行当前Web请求期间发生了未处理的异常.请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息.

异常详细信息:System.Data.EntitySqlException:查询语法无效.,在术语'/',第1行,第2列附近.

这是文件的第一部分.仅针对第一行的注释抛出异常:

/****** Object:  Table [dbo].[User]    Script Date: 10/08/2009 12:14:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[User](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [FirstName] [nvarchar](50) NULL,
    [LastName] [nvarchar](50) NULL,
    [EmailAddress] [nvarchar](100) NULL,
 CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
Run Code Online (Sandbox Code Playgroud)

这个相同的SQL脚本从SQL Management Studio Express执行得很好(实际上该应用程序生成了这个脚本!).它只是Visual Studio自己的Server Explorer查询视图,而且我自己的代码似乎失败了.

Dav*_*vid 11

您需要使用SQL管理类而不是普通的SqlCommand. 此页面显示了如何执行此操作. 如果您尝试自己解析SQL,那么总会有您错过的边缘情况.例如,如果代码中的字符串包含带有前导和尾随回车的单词"GO",该怎么办?

添加以下参考:

  • Microsoft.SqlServer.Smo
  • Microsoft.SqlServer.ConnectionInfo
  • Microsoft.SqlServer.Management.Sdk.Sfc(编辑:不需要此引用)

然后你可以使用这段代码:

string connectionString, scriptText;
SqlConnection sqlConnection = new SqlConnection(connectionString);
ServerConnection svrConnection = new ServerConnection(sqlConnection);
Server server = new Server(svrConnection);
server.ConnectionContext.ExecuteNonQuery(scriptText);
Run Code Online (Sandbox Code Playgroud)


Fre*_*örk 8

这是前一段时间在我的博客上发布的可能解决此问题的代码段:

private static void RunScript(SqlConnection connection, string script)
{
    Regex regex = new Regex(@"\r{0,1}\nGO\r{0,1}\n");
    string[] commands = regex.Split(script);

    for (int i = 0; i < commands.Length; i++)
    {
        if (commands[i] != string.Empty)
        {
            using(SqlCommand command = new SqlCommand(commands[i], connection))
            {
                command.ExecuteNonQuery();
                command.Dispose();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

它将SQL脚本拆分为单独的命令并执行它们中的每一个.我经常使用它来设置生成的SQL脚本的测试数据库.

  • @Mork:`command.Dispose()`不再需要了,因为它在`using`语句中 (2认同)