我似乎无法执行使用DbCommand对象创建数据库的SQL。我究竟做错了什么?这里是我的代码:如何使用SqlConnection执行带注释和GO语句的SQL?
DbConnection connection; // initialized and opened elsewhere
DbCommand cmd = connection.CreateCommand();
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
这里的错误:
The query syntax is not valid., near term '/', line 1, column 2. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.EntitySqlException: The query syntax is not valid., near term '/', line 1, column 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
同样的SQL脚本SQL管理Studio Express的执行就好了(事实上该应用产生的这个脚本!)。它只是Visual Studio自己的服务器资源管理器查询视图,并且来自我自己的代码,这似乎失败了。
注:GO是不是一个SQL命令,它是一个命令至客户端工具(例如OSQL,查询分析器,或在这种情况下SMSS。),以打破脚本转换成单独的“批”。每批次都是对服务器的单独调用。 – 2009-10-08 22:24:46
[执行大型SQL脚本(使用GO命令)]的可能副本](http://stackoverflow.com/questions/40814/execute-a-large-sql-script-with-go-commands) – 2016-06-09 02:15:00