Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SQLCE:为什么我得到;“重复值”;错误?该字段是否启用了标识?_C#_Sql_Sql Server Ce_Sql Server Ce 4 - Fatal编程技术网

C# SQLCE:为什么我得到;“重复值”;错误?该字段是否启用了标识?

C# SQLCE:为什么我得到;“重复值”;错误?该字段是否启用了标识?,c#,sql,sql-server-ce,sql-server-ce-4,C#,Sql,Sql Server Ce,Sql Server Ce 4,在我的查询中,我不使用主键字段,因为已启用标识设置 string sql = @" INSERT INTO [tblTemplates] (personID, hash, data) VALUES (@personID, @hash, @data)"; cmd = new SqlCeCommand(sql, cn); cmd.Parameters.AddWithValue("@personID"

在我的查询中,我不使用主键字段,因为已启用标识设置

   string sql = @"
                  INSERT INTO [tblTemplates] (personID, hash, data) 
                      VALUES (@personID, @hash, @data)";

   cmd = new SqlCeCommand(sql, cn);
   cmd.Parameters.AddWithValue("@personID", newTemplate.personID);
   cmd.Parameters.AddWithValue("@hash", newTemplate.templateHash);
   cmd.Parameters.AddWithValue("@data", newTemplate.templateData);

   cmd.ExecuteNonQuery();
我可以或不能随机插入记录,然后引发异常:

无法将重复值插入唯一索引。
[表名=tblTemplates,约束名=PK\u tblTemplates\u templateID ]

这是表架构:

-- Script Date: 26.08.2011 10:37  - Generated by ExportSqlCe version 3.5.1.5
CREATE TABLE [tblTemplates] (
  [templateID] int NOT NULL  IDENTITY (1,1)
, [hash] nvarchar(100) NOT NULL
, [data] image NOT NULL
, [personID] int NOT NULL
);
GO
ALTER TABLE [tblTemplates] ADD CONSTRAINT [PK__tblTemplates__templateID] PRIMARY KEY ([templateID]);
GO
CREATE INDEX [IDX_tblTemplates_personID] ON [tblTemplates] ([personID] ASC);
GO
CREATE UNIQUE INDEX [UQ__tblTemplates__templateID] ON [tblTemplates] ([templateID] ASC);
GO
为什么会出现此错误?

解决方法:将字段数据类型从int转换为uniqueidentifier有效

我的解决方案尝试:

尝试#1:相同的连接

结果:这似乎是一个无止境的循环

尝试#2:新连接


结果:在进行了几次递归调用后,此尝试有所帮助。

我也遇到了类似的问题。我在我的桌子上使用了
IDENTITY\u INSERT
,就像

在此之后,
MyTable
上的所有插入都会抛出“重复值”错误

这个问题的解决办法是


因为PK列不能有重复的条目。但我相信您的期望远不止这些,但从您的问题来看,这并不清楚。表中是否只有这段代码?或者可能有其他人/应用程序编写它?启动时该表是否为空,或者其中是否有任何数据(可能是从较旧的应用程序的数据库导入的),您不需要UQ_uuuTBLTemplates_uuTemplateId,因为主键是唯一的。此外,如果您的应用程序是多线程的,这可能是从自动分配的ID切换而来,并且使用uniqueidentifier/Guid似乎已经“修复”了我的这个问题。
        bool executed = false;
        int counter = 0;

        while (!executed)
        {
            try
            {
                cmd.ExecuteNonQuery();
                succes = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine("SERVER> (Error) Exception in AddTemplate() {0},{1}", ex.Source, ex.Message);
                System.Threading.Thread.Sleep(100);
            }

            counter++;

        }
            try
            {
                cmd.ExecuteNonQuery();
                succes = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine("SERVER> (Error) Exception in AddTemplate() {0},{1}", ex.Source, ex.Message);
                System.Threading.Thread.Sleep(100);
                AddTemplate(newTemplate); //Warning: Recursive call!
            }
SET IDENTITY_INSERT MyTable ON;
-- some identity insert on MyTable
SET IDENTITY_INSERT MyTable OFF;
var cmd = Connection.CreateCommand();                
cmd.CommandText = "SELECT MAX([Id] ) + 1 from [MyTable]";
object i = cmd.ExecuteScalar();
if (i != null && i is int)
{
    cmd.CommandText = "ALTER TABLE [MyTable] ALTER COLUMN [Id] IDENTITY (" + i + ",1)";
    cmd.ExecuteNonQuery();
}