保存数据时出错 [英] Error in saving the data
问题描述
我有一个错误说
I have an error saying
Additional information: Violation of PRIMARY KEY constraint 'PK__tblPayme__A17005CC944C102D'. Cannot insert duplicate key in object 'dbo.tblPayment'. The duplicate key value is (1).
。
这是什么意思?我该如何解决?谢谢
我尝试过:
.
What does it mean? How can I fix it? Thanks
What I have tried:
<pre>Imports System.Data.SqlClient
Imports System.Data
Public Class Payment
Dim conn As New SqlConnection
Dim cmd As SqlCommand
Dim da As SqlDataAdapter
Dim strcon As String = "Server=DESKTOP-C6IEOUN\SQLEXPRESS;database =NEWCMO; integrated security=True;"
Private Sub dtpDateToday_ValueChanged(sender As Object, e As EventArgs) Handles dtpDateToday.ValueChanged
End Sub
Private Sub Payment_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub btnCompute_Click(sender As Object, e As EventArgs) Handles btnCompute.Click
txtBal.Text = Val(txtAmt.Text) - Val(txtPayment.Text)
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim cmd As New SqlCommand
conn.ConnectionString = strcon
conn.Open()
cmd.Connection = conn
cmd.CommandText = "INSERT INTO tblPayment (patientID,Surname,Firstname,Age,[Total Amount to be Paid],[Type of Payment],Payment,Balance) VALUES(@PID,@SN,@FN,@Age,@Amt,@TypePayment,@Payment,@Balance)"
cmd.Parameters.AddWithValue("@PID", txtPID.Text)
cmd.Parameters.AddWithValue("@SN", txtSN.Text)
cmd.Parameters.AddWithValue("@FN", txtFN.Text)
cmd.Parameters.AddWithValue("@Age", txtAge.Text)
cmd.Parameters.AddWithValue("@Amt", txtAmt.Text)
cmd.Parameters.AddWithValue("@TypePayment", cmbTypeOfPayment.Text)
cmd.Parameters.AddWithValue("@Payment", txtPayment.Text)
cmd.Parameters.AddWithValue("@Balance", txtBal.Text)
cmd.ExecuteNonQuery()
MessageBox.Show("Successful Added Data")
conn.Dispose()
conn.Close()
End Sub
Private Sub Payment_Click(sender As Object, e As EventArgs) Handles Me.Click
''''''''''''''''Searching''''''''''''''''''''''''''
Dim connection As New SqlConnection("Server=DESKTOP-C6IEOUN\SQLEXPRESS;database =NEWCMO; integrated security=True;")
Dim command As New SqlCommand("Select * From tblRegPatient where patientID =@PID", connection)
' command.Parameters.Add("@PID", SqlDbType.Int).Value = txtPID.Text
command.Parameters.AddWithValue("@PID", txtPID.Text)
Dim adapter As New SqlDataAdapter(command)
Dim table As New DataTable()
adapter.Fill(table)
If table.Rows.Count() > 0 Then
txtSN.Text = table.Rows(0)(1).ToString()
txtFN.Text = table.Rows(0)(2).ToString()
txtAge.Text = table.Rows(0)(7).ToString()
Else
MsgBox("No Data Found", vbInformation, "Not Found")
End If
End Sub
End Class
推荐答案
错误消息非常清楚地告诉您,您正试图在Payment表中插入重复值。
查看此CodeProject文章以了解索引 - MS SQL Server中的索引 [ ^ ]
The error message quite clearly tells you that you are trying to insert a duplicate value into the Payment table.
Have a look at this CodeProject article to get an understanding of indexing - Indexes in MS SQL Server[^]
错误消息非常明确:
The error message is pretty explicit:
Cannot insert duplicate key in object 'dbo.tblPayment'. The duplicate key value is (1).
您正在尝试在主键列中插入一个值已经在表中的行 - 可能是 patientID
如果你想改变价值,你不要使用INSERT - 你使用更新:
You are trying to insert a row with a value that is already in the table, in the Primary Key column - probably patientID
If you want to change values, you don't use an INSERT - you use an UPDATE:
UPDATE MyTable SET MyColumn1 = @NewValue1, MyColumn2 = @NewValue2 WHERE MyPrimaryKey = @PID
违反PRIMARY关键约束'PK__tblPayme__A17005CC944C102D'。无法在对象'dbo.tblPayment'中插入重复键。重复的键值是(1)。
Violation of PRIMARY KEY constraint 'PK__tblPayme__A17005CC944C102D'. Cannot insert duplicate key in object 'dbo.tblPayment'. The duplicate key value is (1).
这意味着它的含义。
你试图用一个PRIMARY KEY插入一个新的记录在数据库中,它是被禁止的。
It means what it says.
You are trying to INSERT a new record with a PRIMARY KEY that is already in the database, and it is forbidden.
我该如何解决?
不要为PRIMARY KEY重复使用相同的值。
Do not reuse the same value for the PRIMARY KEY.
这篇关于保存数据时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!