問題描述
我設計了 Webservice api,每次我通過 Webservice 推送數據時,這就是我得到的回報MOV = "違反 PRIMARY KEY 約束 'PK_Vehicle_Transactions'.無法在對象 'dbo.Vehicle_Transactions' 中插入重復鍵.語句已終止."就像 api 不知道它在哪里停止以及在哪里繼續!請在下面查看我的源代碼,謝謝
There's Webservice api that I design, Each time I push data cross the webservice this is what I get in return MOV = "Violation of PRIMARY KEY constraint 'PK_Vehicle_Transactions'. Cannot insert duplicate key in object 'dbo.Vehicle_Transactions'. The statement has been terminated." is like the api doesn't know where it stopped and where to continue! kindly see my source code below thanks
Public Sub uploadVehicle_Transaction()
Try
'do for sync indacator for proper upload in action
Dim VT As New DataTable
VT = New Statn_Sync.DataSetTableAdapters.Vehicle_TransactionsTableAdapter().GetData()
For Each dr As DataRow In VT.Rows
Dim iCount As Integer = 0
Dim MOV As String = comT.insertVehicle_Transaction(Convert.ToInt64(dr("TransactionID")), _
Convert.ToDateTime(dr("Transaction_date")), _
Convert.ToInt32(dr("Bank")), _
Convert.ToString(dr("Teller_number")), _
Convert.ToInt32(dr("Amount")), _
Convert.ToString(dr("Generated_by")), _
Convert.ToString(dr("Station")), _
Convert.ToString(dr("Customer_name")), _
Convert.ToInt32(dr("Transaction_category")), _
Convert.ToString(dr("Deposit_slip")), _
Convert.ToInt32(dr("Sync")), _
Convert.ToDecimal(dr("Penalty")), _
Convert.ToDecimal(dr("OGSG")), _
Convert.ToDecimal(dr("CMR")), _
Convert.ToDecimal(dr("Goshen")), _
Convert.ToDecimal(dr("Insurance")), _
Convert.ToDecimal(dr("OCost")), _
Convert.ToDecimal(dr("OGSG_Renewal")), _
Convert.ToDecimal(dr("De_pulse")))
iCount += 1
Label1.Text = " Auto Sync: " & iCount
'update record
Dim pls As String = dr("TransactionID").ToString
If (pls Is MOV) Then
AddToLog((Convert.ToString(": transferred") & MOV.ToString() & Text) + Environment.NewLine)
vta.UpdateTrans(dr("TransactionID"))
End If
Next
Catch ex As Exception
AddToLog(ex.Message.ToString)
End Try
End Sub
推薦答案
異常已經說明了:違反 PRIMARY KEY 約束 'PK_Vehicle_Transactions'
.表中已經包含一行,其中給出了主鍵 (TransactionID
).主鍵在整個表中是唯一的.
The exception already says it: Violation of PRIMARY KEY constraint 'PK_Vehicle_Transactions'
. The table already contains a row with the Primary Key (TransactionID
) given. A Primary Key is unique throughout the table.
您的問題有多種解決方案:
There are several solutions for your problem:
1) 計算最新的TransactionID
VT = New Statn_Sync.DataSetTableAdapters.Vehicle_TransactionsTableAdapter().GetData()
//Use query to select Max value of TransactionID (something like)
Dim maxPK as Long = 'SELECT MAX(TransactionID) FROM dbo.Vehicle_Transactions'
//Increase the MaxPK with 1 to avoid duplicate key
maxPK = maxPK + 1
For Each dr As DataRow In VT.Rows
Dim iCount As Integer = 0
//Use our variable in the insert
Dim MOV As String = comT.insertVehicle_Transaction((maxPK + iCount), _
Convert.ToDateTime(dr("Transaction_date")), _
2) 在 dbo.Vehicle_Transactions
為此,我參考以下帖子:Auto Increment.這篇文章是為MSSQL 2012的管理工作室做的.但同樣的邏輯適用于早期版本(2008,2005)
For this i refer to the following post: Auto Increment .This post was made for the management studio of MSSQL 2012. But the same logic applies for earlier version (2008,2005)
可以在 StackOverflow 中找到其他解決方案
Other solutions might be found throughout StackOverflow
如果我能提供任何進一步的幫助,請不要猶豫給我簽名!
If i can be of any further assistance, don't hesitate to give me sign!
注意:如果之前的數據對你沒有用,你總是可以在插入之前清除表
使用查詢:DELETE FROM dbo.Vehicle_Transactions
此查詢從表中刪除所有行.盡管您必須警惕任何Forgein Keys
,因為它們可能會導致數據丟失/異常.
Note: If the previous data are of no use to you, you can always clear the table
prior to the insert using the query: DELETE FROM dbo.Vehicle_Transactions
this query removes all rows from the table. Though you have to wary for any Forgein Keys
as they might cause dataloss/exceptions.
這篇關于“違反 PRIMARY KEY 約束 'PK_Vehicle_Transactions'.無法在對象“dbo.Vehicle_Transactions"中插入重復鍵的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!