简体   繁体   English

Sql Server Ce 3.5标识插入

[英]Sql Server Ce 3.5 Identity insert

got an issue with identity columns in Sql Server CE 在Sql Server CE中出现了标识列的问题

when using Server explorer, in VS2008, executing the following script 使用Server explorer时,在VS2008中,执行以下脚本

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

sends the follow message error 'The Set SQL construct or statement is not supported.' 发送以下消息错误'不支持Set SQL构造或语句。 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" 无论如何,当我尝试通过C#做同样的事情时,将该脚本作为命令文本,它无法说错误在“插入关键字”中

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? 我理解,对于SQL SERVER CE,该命令当时只接受一个批处理命令,所以在这种情况下我们有三个命令(它可以与完整的SQLServer一起工作)任何想法?

If you're using SqlCe 3.5 then this should work. 如果您使用的是SqlCe 3.5,那么这应该可行。 However, you need to send it as two separate commands. 但是,您需要将其作为两个单独的命令发送。 You can't separate commands with ";" 你不能用“;”分隔命令 or "GO" in SqlCe. 或者在SqlCe中“GO”。 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();
            }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM