DbCommand 无法在一个 ExecuteNonQuery() 中运行多个 SQL 命令
您好,我遇到以下问题,但无法解决。
使用 DbCommand
我尝试执行此 SQL 语句
Dim strCommnad As String =
"CREATE DEFAULT [dbo].[DOMAIN_XLibPKID_D] AS (0);" + Environment.NewLine +
"CREATE TYPE [dbo].[XLibPKID] FROM BIGINT NOT NULL;" + Environment.NewLine +
"EXEC sp_bindefault 'DOMAIN_XLibPKID_D', 'XLibPKID';"
command.CommandText = strCommnad
command.CommandType = CommandType.Text
command.ExecuteNonQuery()
,但总是收到错误消息
关键字“CREATE”附近的语法不正确。
但是,当我从 strCommand
独立运行每个命令时,一切正常。
我正在使用 VS 2010 Professional 和 SQL Server 2008 R2 Express。
感谢您的任何帮助。
Hello I have the following problem and I can't solve it.
With DbCommand
I'm trying execute this SQL statement
Dim strCommnad As String =
"CREATE DEFAULT [dbo].[DOMAIN_XLibPKID_D] AS (0);" + Environment.NewLine +
"CREATE TYPE [dbo].[XLibPKID] FROM BIGINT NOT NULL;" + Environment.NewLine +
"EXEC sp_bindefault 'DOMAIN_XLibPKID_D', 'XLibPKID';"
command.CommandText = strCommnad
command.CommandType = CommandType.Text
command.ExecuteNonQuery()
but I always get this an error message
Incorrect syntax near the keyword 'CREATE'.
But when I run each command from strCommand
standalone then everything works fine.
I'm using VS 2010 Professional and SQL Server 2008 R2 Express.
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不完全确定你想要做什么 - 但你的方法似乎过于复杂......
你似乎在你的
XLibPKID
列中添加了DEFAULT
子句 -正确的?此ALTER TABLE
语句也应该执行此操作:这只是向您的表添加一个单独的
DEFAULT CONSTRAINT
。I'm not entirely sure what you're trying to do - but your approach seems overly complicated ...
You appear to be adding a
DEFAULT
clause to yourXLibPKID
column - right? ThisALTER TABLE
statement should do that, too:This just adds a separate
DEFAULT CONSTRAINT
to your table.尝试在每个语句之间添加
GO
:Try adding a
GO
between each statement: