您好堆棧溢出社區,單插入語句插入多個值
我有一個瘋狂的問題在我手上。這是我的問題:
1)。執行ExecuteNonQuery()之前,我的數據庫中沒有記錄
2)。執行ExecuteNonQuery()後,我的數據庫中有兩條記錄,儘管我使用了一條插入語句。
我的程序是多線程的。我限制了我的程序,所以只有一個線程正在運行,但仍然出現錯誤。
我會看看其他地區,但我不知道在哪裏搜索或任何原因,爲什麼會發生這種情況。
這是我的程序的基本輪廓。
開始每天一個線程
查找從文件
上傳信息
這裏文件
提取信息是,我用它來上傳數據的代碼(不包括錯誤處理)
string CommandString = INSERT INTO Production_Test.dbo.Transactions
(BoxId, ProcessDate, Batch, BillingElementID,
TransactionDescription, Quantity)
VALUES (@BoxId, @ProcessDate, @Batch,
@BillingElementID, @TransactionDescription, @Quantity)";
SqlCommand Command = new SqlCommand(CommandString, Database);
Command.Parameters.AddWithValue("ProcessDate", CheckPrimaryKeyForNull(this.ProcessDate));
Command.Parameters.AddWithValue("Batch", CheckPrimaryKeyForNull(this.Batch));
Command.Parameters.AddWithValue("BoxId", CheckPrimaryKeyForNull(this.BoxId));
Command.Parameters.AddWithValue("BillingElementId", CheckPrimaryKeyForNull(this.BillingElementId));
Command.Parameters.AddWithValue("TransactionDescription", CheckPrimaryKeyForNull(this.TransactionDescription));
Command.Parameters.AddWithValue("Quantity", CheckIfNullAddZero(this.Quantity));
Command.ExecuteNonQuery();
我只使用一個插入語句,但它插入兩條記錄。有人知道爲什麼會發生這種情況嗎?
謝謝,
Darren。
編輯:這是我的代碼的多線程部分。
for (DateTime CurrentDate = BeginningDate; CurrentDate <= EndingDate; CurrentDate = CurrentDate.AddDays(1))
{
DateTime TempCurrentDate = CurrentDate;
FileThreads[ThreadCounter] = new Thread(() => RipInformationFromFiles.Process(Parameters, TempCurrentDate, ArgsIndex));
FileThreads[ThreadCounter].Start();
ThreadCounter++;
}
foreach (Thread ThisThread in FileThreads)
{
if (ThisThread != null)
{
ThisThread.Join();
}
}
然後,每個線程通過一定的路徑查找並更新信息。
我唯一的一次鎖定是當我調用一個類的上傳方法。
lock (Locker)
{
ProductionDatabase.Upload();
}
我用這static readonly object Locker = new object();
爲我的鎖。
插入代碼毫無價值...顯然,問題在於您使用多線程。 –
如果您沒有發佈相關代碼以及如何處理它,我們無法幫助您進行線程化。 – Romoku
另外,檢查表格定義是否有可能的觸發器。 – GolfWolf