我似乎無法執行使用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