database - Duplicate value exception while inserting into an autoincremental-PK table -
i experienced sqlceexception
: "a duplicate value cannot inserted unique index" while inserting record in table (sqlce 3.5).
it seems insert statement violates primary key constraint. however, on relevant table, pk has been defined autoincrementing identity:
[id] int identity(1,1) not null,
the exception not systematical: happens once in while.
what cause of exception?
doesn't identity(1,1)
guarantee unique id each insert, it?
sqlce bug? if so, how circumvented?
here exception trace (it explicitly refers pk_id constraint):
system.data.sqlserverce.sqlceexception: duplicate value cannot inserted unique index. [ table name = historicaldata,constraint name = pk_id ] @ system.data.sqlserverce.sqlcecommand.processresults(int32 hr) @ system.data.sqlserverce.sqlcecommand.executecommandtext(intptr& pcursor, boolean& isbasetablecursor) @ system.data.sqlserverce.sqlcecommand.executecommand(commandbehavior behavior, string method, resultsetoptions options) @ system.data.sqlserverce.sqlcecommand.executenonquery() @ invensys.compact.persistence.sqlce.plantdata.savehistoricalrecord(historicalrecord record) @ ...
table definition is:
create table [historicaldata] ( [id] int identity(1,1) not null, [timestamp] datetime not null, [type] smallint not null, [idlastall] int, [idref] int not null, [binarylength] smallint not null, [data00] varbinary(500), [data01] varbinary(500), [data02] varbinary(500), [data03] varbinary(500), [data04] varbinary(500), [data05] varbinary(500), [data06] varbinary(500), [data07] varbinary(500), [data08] varbinary(500), [data09] varbinary(500)); -- create primary key constraints alter table [historicaldata] add constraint [pk_id] primary key ([id]); -- create indexes create index [idx_type_timestamp] on [historicaldata] ([type] asc, [timestamp] asc); create index [idx_timestamp] on [historicaldata] ([timestamp] asc);
the insert performed via sqlcecommand. here code (c#):
_insertrecordcommand = new sqlcecommand { connection = _connection, commandtype = commandtype.text, commandtext = historical_data_insert }; _insertrecordcommand.parameters.add("@timestamp", sqldbtype.datetime); _insertrecordcommand.parameters.add("@type", sqldbtype.smallint); _insertrecordcommand.parameters.add("@idlastall", sqldbtype.int); _insertrecordcommand.parameters.add("@idref", sqldbtype.int); _insertrecordcommand.parameters.add("@binarylength", sqldbtype.int); (var = 0; < data_block_number; i++) { _insertrecordcommand.parameters.add(dataparameternames[i], sqldbtype.varbinary, data_block_size); } _insertrecordcommand.prepare(); // here parameters filled. _insertrecordcommand.executenonquery();
i left out how parameters filled. nevertheless, there no [id] (the primary key) parameter.
Comments
Post a Comment