我以前使用过以下的脚本,它可以正常工作:
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);
答案 0 :(得分:1)
GO
不是SQL关键字;它由SQL Server Management Studio解释。
您需要将每个批处理作为单独的ExecuteSql
调用发送,或使用SQL Server管理对象(SMO)。
请参阅my related question以获得一些帮助。