0

我以前使用过以下运行良好的脚本:

    public const string application =
@"SET IDENTITY_INSERT Application ON
INSERT INTO Application(ApplicationId, Name, ModifiedDate) VALUES (1, 'x', getdate() );
INSERT INTO Application(ApplicationId, Name, ModifiedDate) VALUES (2, 'y', getdate() );
INSERT INTO Application(ApplicationId, Name, ModifiedDate) VALUES (3, 'z', getdate() );
SET IDENTITY_INSERT Application OFF
DBCC CHECKIDENT( 'Application', RESEED )";

现在我想执行 DDL,但我收到一条错误消息:

    public const string dropCreate =
        @"
        GO
        ALTER TABLE [dbo].[TestAccount] DROP CONSTRAINT [TestAccount_Application]
        GO
        DROP TABLE [dbo].[TestAccount]
        GO
        SET ANSI_NULLS ON
        GO        
        SET QUOTED_IDENTIFIER ON
        GO  
        CREATE TABLE [dbo].[TestAccount](
            [TestAccountId] [int] IDENTITY(1,1) NOT NULL,
            [Name] [nvarchar](35) NOT NULL,
            [ApplicationId] [int] NOT NULL,
            [RowVersion] [timestamp] NOT NULL,
            [ModifiedDate] [datetime] NOT NULL,
        PRIMARY KEY CLUSTERED 
        (
            [TestAccountId] 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
        ALTER TABLE [dbo].[TestAccount]  WITH CHECK ADD  CONSTRAINT [TestAccount_Application] FOREIGN KEY([ApplicationId])
        REFERENCES [dbo].[Application] ([ApplicationId])
        ON DELETE CASCADE
        GO       
        ALTER TABLE [dbo].[TestAccount] CHECK CONSTRAINT [TestAccount_Application]
        GO";
}

   HResult=-2146232060
   Message=Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.

我可以从 C# 执行 DDL。我正在使用的代码如下所示:

context.Database.ExecuteSqlCommand(TestAccountSQL.dropCreate);
4

1 回答 1

1

GO不是 SQL 关键字;它由 SQL Server Management Studio 解释。

您需要将每个批次作为单独的ExecuteSql调用发送,或使用 SQL Server 管理对象 (SMO)。

请参阅我的相关问题以获得一些帮助。

于 2013-03-25T04:18:16.620 回答