Sql Server Ce 3.5 身份插入

发布于 2024-07-15 10:52:22 字数 418 浏览 6 评论 0原文

在 VS2008 中使用服务器资源管理器时, Sql Server CE 中的标识列出现问题

,执行以下脚本

SET IDENTITY_INSERT testTable ON; 插入 testTable (id,name) 值 (1,'Something') SET IDENTITY_INSERT testTable ON;

发送以下消息错误 “不支持 Set SQL 构造或语句。” 但随后插入行就好了?!?!?!

无论如何,当我尝试通过 C# 做同样的事情时,将该脚本作为命令文本 它失败了,说错误出现在“插入关键字”中

我知道,针对 SQL SERVER CE,该命令一次只接受一个批处理命令 所以在这种情况下我们有三个命令(它可以与完整的 SQLServer 一起使用) 任何想法?

got an issue with identity columns in Sql Server CE

when using Server explorer, in VS2008, executing the following script

SET IDENTITY_INSERT testTable ON;
Insert into testTable (id,name) values (1,'Something')
SET IDENTITY_INSERT testTable ON;

sends the follow message error
'The Set SQL construct or statement is not supported.'
but then inserts the row fine ?!?!?!

anyway, when I try to do the same thing through C#, giving that script as a command text
it fails saying the error was in the "Insert key word"

I understand that against SQL SERVER CE the command only accepts one batch command at the time
so in that case we have three commands (it would work with the full SQLServer)
any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

↙厌世 2024-07-22 10:52:22

如果您使用的是 SqlCe 3.5,那么这应该可以工作。 但是,您需要将其作为两个单独的命令发送。 不能用“;”分隔命令 或 SqlCe 中的“GO”。 相反,你需要做这样的事情:

            SqlCeConnection connection = new SqlCeConnection(connectionString);
            SqlCeCommand identChangeCommand = connection.CreateCommand();
            identChange.CommandText = "SET IDENTITY_INSERT SomeTable ON";
            SqlCeCommand cmd = connection.CreateCommand();
            cmd.CommandText = "INSERT INTO testTable (Id, column1, column2..) VALUES (10,val1,val2...)";
            try
            {
                connection.Open();
                identChange.ExecuteNonQuery();
                cmd.ExecuteNonQuery();
            }

            catch (SqlCeException ex)
            {
                //log ex
            }
            finally
            {
                connection.Close();
            }

If you're using SqlCe 3.5 then this should work. However, you need to send it as two separate commands. You can't separate commands with ";" or "GO" in SqlCe. Rather, you need to do something like this:

            SqlCeConnection connection = new SqlCeConnection(connectionString);
            SqlCeCommand identChangeCommand = connection.CreateCommand();
            identChange.CommandText = "SET IDENTITY_INSERT SomeTable ON";
            SqlCeCommand cmd = connection.CreateCommand();
            cmd.CommandText = "INSERT INTO testTable (Id, column1, column2..) VALUES (10,val1,val2...)";
            try
            {
                connection.Open();
                identChange.ExecuteNonQuery();
                cmd.ExecuteNonQuery();
            }

            catch (SqlCeException ex)
            {
                //log ex
            }
            finally
            {
                connection.Close();
            }
且行且努力 2024-07-22 10:52:22

尝试

SET IDENTITY_INSERT testTable ON; 
Insert into testTable (id,name) values (1,'Something');
SET IDENTITY_INSERT testTable OFF;

SET IDENTITY_INSERT testTable ON
go
Insert into testTable (id,name) values (1,'Something')
go
SET IDENTITY_INSERT testTable OFF
go

TRY

SET IDENTITY_INSERT testTable ON; 
Insert into testTable (id,name) values (1,'Something');
SET IDENTITY_INSERT testTable OFF;

OR

SET IDENTITY_INSERT testTable ON
go
Insert into testTable (id,name) values (1,'Something')
go
SET IDENTITY_INSERT testTable OFF
go
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文