我可以无错误地执行Access SQL INSERT
语句,但是当我尝试使用以下代码进行UPDATE
时,它将引发错误:
“查询表达式中字符串的语法错误”。
为什么会发生这种情况,我该如何解决?
这是我的代码。
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
If txtName.Text = Nothing Or dtpDateBirth.Text = Nothing Or txtPhoneNumber.Text = Nothing Or txtEmail.Text = Nothing Or txtID.Text = Nothing Or mtbNPWP.Text = Nothing Or rtbAddress.Text = Nothing Or txtVillage.Text = Nothing Or txtSubDistrict.Text = Nothing Or txtCity.Text = Nothing Or txtPostalCode.Text = Nothing Or txtEmergencyName.Text = Nothing Or txtEmergencyNumber.Text = Nothing Or txtEmergencyEmail.Text = Nothing Then
MsgBox("No data has been update", MsgBoxStyle.Information, "No data edit")
Exit Sub
Else
Call koneksi()
cmd = New OleDb.OleDbCommand("update t_customer set name='" & txtName.Text & "', date_birth='" & dtpDateBirth.Text & "', phone_number='" & txtPhoneNumber.Text & "', email='" & txtEmail.Text & "', id='" & txtID.Text & "', npwp='" & mtbNPWP.Text & "', address='" & rtbAddress.Text & "', village='" & txtVillage.Text & "', sub_district='" & txtSubDistrict.Text & "', city='" & txtCity.Text & "', postal_code='" & txtPostalCode.Text & "', gender='" & gen & "', emergency_name='" & txtEmergencyName.Text & "', phone_emergency='" & txtPhoneNumber.Text & "', email_emergency='" & txtEmergencyEmail.Text & "' where [codeCust]='" & txtIDCust.Text, conn)
cmd.ExecuteNonQuery()
MsgBox("Update data success!", MsgBoxStyle.Information, "Update")
End If
Call disabled()
Call showData()
End Sub
答案 0 :(得分:2)
正如HansUp建议使用链接一样,您应该使用参数。同样,您也没有处置该命令。另外,AFAIK(我认为)需要先打开连接,然后才能执行ExecuteNonQuery。至于您遇到的数据不匹配问题,我认为这是因为假设ID为INT,在where子句中有一个不应该存在的'。
function addChipWidget(editor) {
editor.widgets.add('simplebox', {
button: 'widget button',
allowedContent: 'span(chip)[contenteditable]',
template: '<span contenteditable="false" class="chip">Smart</chip>',
upcast: function(element) {
return element.name == 'span' && element.hasClass('chip');
}
});
}
答案 1 :(得分:0)
感谢所有帮助我的人。我非常感谢你。这个问题已被解决。 这是我的完成代码,我终于可以用来更新数据库了。
Sub updateData()
Call gender()
If String.IsNullOrEmpty(txtName.Text) OrElse String.IsNullOrEmpty(dtpDateBirth.Text) OrElse String.IsNullOrEmpty(txtPhoneNumber.Text) OrElse String.IsNullOrEmpty(txtEmail.Text) OrElse String.IsNullOrEmpty(txtID.Text) OrElse String.IsNullOrEmpty(mtbNPWP.Text) OrElse String.IsNullOrEmpty(rtbAddress.Text) OrElse String.IsNullOrEmpty(txtVillage.Text) OrElse String.IsNullOrEmpty(txtSubDistrict.Text) OrElse String.IsNullOrEmpty(txtCity.Text) OrElse String.IsNullOrEmpty(txtPostalCode.Text) OrElse String.IsNullOrEmpty(txtEmergencyName.Text) OrElse String.IsNullOrEmpty(txtEmergencyNumber.Text) OrElse String.IsNullOrEmpty(txtEmergencyEmail.Text) Then
MsgBox("No data has been update", MsgBoxStyle.Information, "No data edit")
Exit Sub
Else
Try
Call koneksi()
Using cmd = New OleDb.OleDbCommand("update t_customer set name='" & txtName.Text & "', date_birth='" & dtpDateBirth.Text & "', phone_number='" & txtPhoneNumber.Text & "', email='" & txtEmail.Text & "', id='" & txtID.Text & "', npwp='" & mtbNPWP.Text & "', address='" & rtbAddress.Text & "', village='" & txtVillage.Text & "', sub_district='" & txtSubDistrict.Text & "', city='" & txtCity.Text & "', postal_code='" & txtPostalCode.Text & "', gender='" & gen & "', emergency_name='" & txtEmergencyName.Text & "', phone_emergency='" & txtPhoneNumber.Text & "', email_emergency='" & txtEmergencyEmail.Text & "' where [codeCust]=@codeCust", conn)
cmd.Parameters.Add("codeCust", CType(txtIDCust.Text, String))
cmd.ExecuteNonQuery()
conn.Close()
MsgBox("Update data success!", MsgBoxStyle.Information, "Update")
End Using
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End Sub
因为我使用了单选按钮来显示性别选项,所以我添加了它,以便可以更改性别。
Private Sub gender()
Call koneksi()
Dim str As String
str = "Insert into t_customer([gender]) Values (?)"
Dim cmd As OleDbCommand = New OleDbCommand(str, conn)
If rdnMale.Checked = True Then
gen = rdnMale.Text
cmd.Parameters.Add(New OleDbParameter("gender", CType(gen, String)))
Else
gen = rdnFemale.Text
End If
End Sub