我通过VB代码更新了我的MS Access数据库:
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
Dim query As String = "UPDATE Employee SET EmpName = @EmpName , MobNo = @MobNo , Email_ID = @Email , EmpAdd = @EmpAdd , FatName = @FatName , Sex = @Sex , Pan = @Pan , AccNo = @AccNo , BankName = @BankName , MICR = @MICR , BasicPay = @BasicPay , passwd = @passwd , DOB = @DOB WHERE id = @id "
Try
Call OpenConn()
Dim cmd As New OleDbCommand(query, conn)
cmd.Parameters.Add("@MobNo", OleDbType.VarWChar, 13, "MobNo").Value = txtMobNo.Text
cmd.Parameters.Add("@Email", OleDbType.VarWChar, 50, "Email_ID").Value = txtEmail.Text
cmd.Parameters.Add("@EmpName", OleDbType.VarWChar, 255, "EmpName").Value = txtName.Text
cmd.Parameters.Add("@DOB", OleDbType.VarWChar, 10, "DOB").Value = txtDOB.Value
cmd.Parameters.Add("@EmpAdd", OleDbType.VarWChar, 255, "EmpAdd").Value = txtAddress.Text
cmd.Parameters.Add("@FatName", OleDbType.VarWChar, 255, "FatName").Value = txtFatName.Text
cmd.Parameters.Add("@Sex", OleDbType.VarWChar, 6, "Sex").Value = cboSex.Text
cmd.Parameters.Add("@Nationality", OleDbType.VarWChar, 30, "Nationality").Value = txtNationality.Text
cmd.Parameters.Add("@Pan", OleDbType.VarWChar, 20, "Pan").Value = txtPan.Text
cmd.Parameters.Add("@AccNo", OleDbType.VarWChar, 20, "AccNo").Value = txtAccNo.Text
cmd.Parameters.Add("@BankName", OleDbType.VarWChar, 50, "BankName").Value = txtBankName.Text
cmd.Parameters.Add("@MICR", OleDbType.VarWChar, 20, "MICR").Value = txtMICR.Text
cmd.Parameters.Add("@DedRate", OleDbType.VarWChar, 3, "DedRate").Value = txtDedRate.Text
cmd.Parameters.Add("@BasicPay", OleDbType.VarWChar, 10, "BasicPay").Value = txtBasicPay.Text
'cmd.Parameters.AddWithValue("@CL", Convert.ToInt64(txtNoOfCl.Text))
'cmd.Parameters.AddWithValue("@ML", Convert.ToInt64(txtNoOfMl.Text))
cmd.Parameters.Add("@passwd", OleDbType.VarWChar, 20, "passwd").Value = txtPasswd.Text
cmd.Parameters.Add("@id", OleDbType.BigInt, 3, "id").Value = Val(txtId.Text)
cmd.Prepare()
cmd.ExecuteNonQuery()
Call CloseConn()
MsgBox("Data Updated Successfully", vbInformation, "Update")
DisableTxt(Me)
btnUpdate.Visible = False
btnEdit.Enabled = False
btnEdit.Visible = False
ClearTxt(Me)
Catch ex As Exception
MsgBox("Fatal error occured :" & vbNewLine & "source : " & vbNewLine & ex.Source & vbNewLine & "messege :" & vbNewLine & ex.ToString)
End Try
End Sub
但更新无效。有什么想法吗?
答案 0 :(得分:2)
使用OleDB,您无法使用命名参数。你必须使用?作为占位符,参数的顺序很重要。
见这里:http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.parameters(v=vs.110).aspx