在使用.Net将数据插入MySQL数据库时,您可以向我提出如何跟踪我所遇到的错误的建议吗?
错误消息是:
MySql.Data.MySqlClient.MySqlException: Duplicate entry '26012' for key 'StockNumber_Number_UNIQUE'
查看日志证明尚未插入StockNumber_Number of 26012。
正在使用的产品。
该代码适用于3000 - 5000次导入。
导入的记录导致完整运行中的问题,如果只是单独导入,则可以正常工作。如果我以不同的方式对要导入的数据进行排序,我也看到了其他记录的错误。
尝试使用和不使用交易进行导入。
已经将地狱记录下来了。
用于创建表的SQL命令:
CREATE TABLE `RareItems_RareItems` (
`RareItemKey` CHAR(36) NOT NULL PRIMARY KEY,
`StockNumber_Text` VARCHAR(7) NOT NULL,
`StockNumber_Number` INT NOT NULL AUTO_INCREMENT,
UNIQUE INDEX `StockNumber_Number_UNIQUE` (`StockNumber_Number` ASC),
`OurPercentage` NUMERIC ,
`SellPrice` NUMERIC(19, 2) ,
`Author` VARCHAR(250) ,
`CatchWord` VARCHAR(250) ,
`Title` TEXT ,
`Publisher` VARCHAR(250) ,
`InternalNote` VARCHAR(250) ,
`DateOfPublishing` VARCHAR(250) ,
`ExternalNote` LONGTEXT ,
`Description` LONGTEXT ,
`Scrap` LONGTEXT ,
`SuppressionKey` CHAR(36) NOT NULL,
`TypeKey` CHAR(36) NOT NULL,
`CatalogueStatusKey` CHAR(36) NOT NULL,
`CatalogueRevisedDate` DATETIME ,
`CatalogueRevisedByKey` CHAR(36) NOT NULL,
`CatalogueToBeRevisedByKey` CHAR(36) NOT NULL,
`DontInsure` BIT NOT NULL,
`ExtraCosts` NUMERIC(19, 2) ,
`IsWebReady` BIT NOT NULL,
`LocationKey` CHAR(36) NOT NULL,
`LanguageKey` CHAR(36) NOT NULL,
`CatalogueDescription` VARCHAR(250) ,
`PlacePublished` VARCHAR(250) ,
`ToDo` LONGTEXT ,
`Headline` VARCHAR(250) ,
`DepartmentKey` CHAR(36) NOT NULL,
`Temp1` INT ,
`Temp2` INT ,
`Temp3` VARCHAR(250) ,
`Temp4` VARCHAR(250) ,
`InternetStatusKey` CHAR(36) NOT NULL,
`InternetStatusInfo` LONGTEXT ,
`PurchaseKey` CHAR(36) NOT NULL,
`ConsignmentKey` CHAR(36) ,
`IsSold` BIT NOT NULL,
`RowCreated` DATETIME NOT NULL,
`RowModified` DATETIME NOT NULL
);
用于插入记录的SQL命令和参数:
INSERT INTO `RareItems_RareItems`
(`RareItemKey`, `StockNumber_Text`, `StockNumber_Number`, `OurPercentage`, `SellPrice`, `Author`, `CatchWord`, `Title`, `Publisher`, `InternalNote`, `DateOfPublishing`, `ExternalNote`, `Description`, `Scrap`, `SuppressionKey`, `TypeKey`, `CatalogueStatusKey`, `CatalogueRevisedDate`, `CatalogueRevisedByKey`, `CatalogueToBeRevisedByKey`, `DontInsure`, `ExtraCosts`, `IsWebReady`, `LocationKey`, `LanguageKey`, `CatalogueDescription`, `PlacePublished`, `ToDo`, `Headline`, `DepartmentKey`, `Temp1`, `Temp2`, `Temp3`, `Temp4`, `InternetStatusKey`, `InternetStatusInfo`, `PurchaseKey`, `ConsignmentKey`, `IsSold`, `RowCreated`, `RowModified`)
VALUES
(@RareItemKey, @StockNumber_Text, @StockNumber_Number, @OurPercentage, @SellPrice, @Author, @CatchWord, @Title, @Publisher, @InternalNote, @DateOfPublishing, @ExternalNote, @Description, @Scrap, @SuppressionKey, @TypeKey, @CatalogueStatusKey, @CatalogueRevisedDate, @CatalogueRevisedByKey, @CatalogueToBeRevisedByKey, @DontInsure, @ExtraCosts, @IsWebReady, @LocationKey, @LanguageKey, @CatalogueDescription, @PlacePublished, @ToDo, @Headline, @DepartmentKey, @Temp1, @Temp2, @Temp3, @Temp4, @InternetStatusKey, @InternetStatusInfo, @PurchaseKey, @ConsignmentKey, @IsSold, @RowCreated, @RowModified)
@RareItemKey = 0b625bd6-776d-43d6-9405-e97159d172a6
@StockNumber_Text = 199305
@StockNumber_Number = 26012
@OurPercentage = 22.5
@SellPrice = 1250
@Author = SPARRMAN, Anders.
@CatchWord = COOK: SECOND VOYAGE
@Title = A Voyage Round the World with Captain James Cook in H.M.S. Resolution… Introduction and notes by Owen Rutter, wood engravings by Peter Barker-Mill.
@Publisher =
@InternalNote =
@DateOfPublishing = 1944
@ExternalNote = The first English translation of Sparrman’s narrative, which had originally been published in Sweden in 1802-1818, and the only complete version of his account to appear in English. The eighteenth-century translation had appeared some time before the Swedish publication of the final sections of his account. Sparrman’s observant and well-written narrative of the second voyage contains much that appears nowhere else, emphasising naturally his interests in medicine, health, and natural history.<br><br>One of 350 numbered copies: a handsomely produced and beautifully illustrated work.
@Description = Small folio, wood-engravings in the text; original olive glazed cloth, top edges gilt, a very good copy. London, Golden Cockerel Press, 1944.
@Scrap =
@SuppressionKey = 00000000-0000-0000-0000-000000000000
@TypeKey = 93f58155-7471-46ad-84c5-262ab9dd37e8
@CatalogueStatusKey = 00000000-0000-0000-0000-000000000003
@CatalogueRevisedDate =
@CatalogueRevisedByKey = c4f6fc06-956d-44c4-b393-0d5462cbffec
@CatalogueToBeRevisedByKey = 00000000-0000-0000-0000-000000000000
@DontInsure = False
@ExtraCosts =
@IsWebReady = False
@LocationKey = 00000000-0000-0000-0000-000000000000
@LanguageKey = 00000000-0000-0000-0000-000000000000
@CatalogueDescription =
@PlacePublished = Golden Cockerel Press
@ToDo =
@Headline =
@DepartmentKey = 529578a3-9189-40de-b656-eef9039d00b8
@Temp1 =
@Temp2 =
@Temp3 =
@Temp4 = v
@InternetStatusKey = 00000000-0000-0000-0000-000000000000
@InternetStatusInfo =
@PurchaseKey = 00000000-0000-0000-0000-000000000000
@ConsignmentKey =
@IsSold = True
@RowCreated = 8/04/2010 8:49:16 PM
@RowModified = 8/04/2010 8:49:16 PM
有关导致错误的原因和/或如何追踪导致问题的原因的建议?
答案 0 :(得分:1)
道歉,我是MSSQL的人,所以可能在这里不合适,但为什么要插入StockNumber_Number
作为插入的一部分。它的auto_increment
字段肯定会为您创建。
从插入中删除此字段或不将其设为auto_increment
,问题就会消失。
答案 1 :(得分:0)
乍一看,STOCK_NUMBER是自动增量即。每次插入新记录时,此列的值将从前一个值增加1。由数据库插入。
在这种情况下,为什么需要将其作为参数传递给查询?
如果你确实需要传递它,我会删除自动增量&amp;保持列的独特性......
HTH。
答案 2 :(得分:0)
从INSERT语句中删除Stock_Number,你应该没问题。
答案 3 :(得分:0)
由于所有其他答案都表明问题是将值插入AUTO_INCREMENT列StockNumber_Number。
在错误发生之前我能够完成几千行似乎很奇怪,但这是我所需要的解决方法。
ALTER TABLE `rareitems_rareitems` CHANGE COLUMN `StockNumber_Number` `StockNumber_Number` INT(11) NOT NULL;
...
do the import
...
ALTER TABLE `rareitems_rareitems` CHANGE COLUMN `StockNumber_Number` `StockNumber_Number` INT(11) NOT NULL AUTO_INCREMENT;