当前,当我尝试编辑客户信息时,它会将每个客户信息都编辑为同一内容。
我没有收到任何错误,例如,如果我将客户的名字更改为John,那么它将每个客户都更改为John
我使用“保存”按钮添加和编辑客户 因此,这是我的代码,所有帮助将不胜感激。谢谢
Private Sub btnSave_Customerform_Click(sender As Object, e As EventArgs) Handles btnSave_Customerform.Click
If DbConnect() Then
Dim SQLCmd As New OleDbCommand
If CurrentCustomerID = -1 Then
' ' ADDING NEW CUSTOMER
With SQLCmd
.Connection = cn
.CommandText = "Insert into Tbl_Customer ([First name] , [Secondname] , [Address] , [Postcode], [DOB], [Phone number]) " &
"Values ([@First name], [@Second name], [@Address], [@Postcode], [@DOB], [@Phone number])"
.Parameters.AddWithValue("@First name", txtFirstname.Text)
.Parameters.AddWithValue("@Second name", txtLastname_Customerform.Text)
.Parameters.AddWithValue("@Address", txtAddress_Customerform.Text)
.Parameters.AddWithValue("@Postcode", txtPostcode_Customerform.Text)
.Parameters.AddWithValue("@DOB", datDOB_Customerform.Value.Date)
.Parameters.AddWithValue("@Phone number", txtPhonenumber_Customerform.Text)
'Validations
If txtFirstname_Customerform.Text.Length > 30 Then
MessageBox.Show("ERROR")
End If
.ExecuteNonQuery()
' Generates Customer ID
.CommandText = "Select @@Identity"
CurrentCustomerID = .ExecuteScalar
Txt_CustID.Text = CurrentCustomerID
End With
Else
'EDIT CUSTOMER
With SQLCmd
.Connection = cn
.CommandText = "Update Tbl_Customer " &
"Set Secondname = @Secondname, " &
"First name= @First name, " &
"DOB = @DOB, " &
"PostCode = @PostCode " &
"Where [CurrentCustomerID] = @CurrentCustomerID"
.Parameters.AddWithValue("@Secondname", txtLastname_Customerform.Text)
.Parameters.AddWithValue("@First name", txtFirstname.Text)
.Parameters.AddWithValue("@DOB", datDOB_Customerform.Value.Date)
.Parameters.AddWithValue("@PostCode", txtPostcode_Customerform.Text)
.Parameters.AddWithValue("@Phone number", txtPhonenumber_Customerform.Text)
.Parameters.AddWithValue("@CurrentCustomerID", CurrentCustomerID)
.ExecuteNonQuery()
End With
cn.Close()
End If
End If
End Sub
我从另一个项目中重新使用了此代码,这里的代码似乎正常工作
Private Sub butSave_Click(sender As Object, e As EventArgs) Handles butSave.Click
If DbConnect() Then
Dim SQLCmd As New OleDbCommand
If CurrentStudentID = -1 Then
'--------------------
'Adding a New Student
'--------------------
With SQLCmd
.Connection = cn
.CommandText = "Insert into Students (Surname, FirstNames, DateOfBirth, Postcode) " & "Values (@Surname, @FirstNames, @DateOfBirth, @PostCode)"
.Parameters.AddWithValue("@Surname", TxtSurname.Text)
.Parameters.AddWithValue("@FirstNames", TxtFirstName.Text)
.Parameters.AddWithValue("@DateOfBirth", DatDOB.Value.Date)
.Parameters.AddWithValue("@PostCode", TxtPostcode.Text)
.ExecuteNonQuery()
'Now get the auto generated ID of this new student
.CommandText = "Select @@Identity"
CurrentStudentID = SQLCmd.ExecuteScalar
lblStudentID.Text = CurrentStudentID
End With
Else
'--------------------
'Editing an exisiting Student
'--------------------
With SQLCmd
.Connection = cn
.CommandText = "Update Students " &
"Set Surname = @Surname, " &
"FirstNames = @FirstNames, " &
"DateOfBirth = @DateOfBirth, " &
"PostCode = @PostCode " &
"Where StudentID = @CurrentStudentID"
.Parameters.AddWithValue("@Surname", TxtSurname.Text)
.Parameters.AddWithValue("@FirstNames", TxtFirstName.Text)
.Parameters.AddWithValue("@DateOfBirth", DatDOB.Value.Date)
.Parameters.AddWithValue("@PostCode", TxtPostcode.Text)
.Parameters.AddWithValue("@CurrentStudentID", CurrentStudentID)
.ExecuteNonQuery()
End With
End If
cn.Close()
End If
End Sub