[英]Need help to debug application using .Net and MySQL
對於使用.Net將數據插入MySQL數據庫時,如何跟蹤我遇到的錯誤,您能給我什么建議?
錯誤消息是:
MySql.Data.MySqlClient.MySqlException: Duplicate entry '26012' for key 'StockNumber_Number_UNIQUE'
查看日志可證明尚未插入StockNumber_Number 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
有關導致錯誤的原因和/或如何找出導致問題的原因的建議?
抱歉,我是MSSQL的生父,所以可能不在這里,但是為什么要插入StockNumber_Number
作為插入的一部分。 它是一個auto_increment
字段,因此一定會為您創建。
要么從您的插入內容中刪除該字段,要么不使其成為auto_increment
,問題就會消失。
乍一看,STOCK_NUMBER是自動遞增的,即 每次插入新記錄時,此列的值將從上一個值開始增加1,並由數據庫插入。
在這種情況下,為什么需要將其作為參數傳遞給查詢?
如果您確實需要傳遞它,我將刪除自動增量並保持列的唯一性...
HTH。
從INSERT語句中刪除Stock_Number,您應該沒問題。
正如所有其他答案都表明的那樣,問題是在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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.