UPDATE语句的语法异常,请问高人
UPDATE语句的语法错误,请教高人
初学ACCESS数据库,编写一个例程,发现每次做修改动作时候(update),都会报“UPDATE语句的语法错误”这个错误,同时datagrid1中数据显示已经更改,但是其实并没有把修改后保存于数据库ACCESS中。请教高人,我这是错在哪儿?该怎么修改? 本人现用的是VS2013版本。
代码如下:
Private Sub bupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bupdate.Click
If Txtname.Text = "" Or Txtpassword.Text = "" Then
MsgBox("请首先在输入框中输入姓名及密码", MsgBoxStyle.Exclamation, "无法修改")
Exit Sub
End If
Dim curid As Integer
Dim curname As String
Dim curpass As String
curid = DataGrid1.Item(DataGrid1.CurrentRowIndex, 0)
curname = DataGrid1.Item(DataGrid1.CurrentRowIndex, 1)
curpass = DataGrid1.Item(DataGrid1.CurrentRowIndex, 2)
Dim infostring As String
infostring = "现在试图修改ID为" + curid.ToString + "的记录" + ControlChars.Cr
infostring += "姓名" + curname + ControlChars.Cr
infostring += "密码:" + curpass + ControlChars.Cr
infostring += "确定修改吗?"
Dim r As MsgBoxResult
r = MsgBox(infostring, MsgBoxStyle.YesNo, "确认修改")
If r = MsgBoxResult.No Then
Exit Sub
End If
Try
myds.Tables("login").Rows(DataGrid1.CurrentRowIndex).Item("name") = Txtname.Text 'myds(dataset).table获得name.text
myds.Tables("login").Rows(DataGrid1.CurrentRowIndex).Item("password") = Txtpassword.Text
Dim cmdstring As String
cmdstring = "update login"
cmdstring += "set name=?,password =?"
cmdstring += "where id=" + curid.ToString
oledbcmd.CommandText = cmdstring
oledbcmd.Parameters.Clear()
oledbcmd.Parameters.Add("pname", Txtname.Text)
oledbcmd.Parameters.Add("ppass", Txtpassword.Text)
oledbda.UpdateCommand = oledbcmd
oledbda.Update(myds, "login")
Catch ex As OleDb.OleDbException
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
------解决思路----------------------
set
where
前面都没有空格
参数应该用 @参数名 而不是问号
初学ACCESS数据库,编写一个例程,发现每次做修改动作时候(update),都会报“UPDATE语句的语法错误”这个错误,同时datagrid1中数据显示已经更改,但是其实并没有把修改后保存于数据库ACCESS中。请教高人,我这是错在哪儿?该怎么修改? 本人现用的是VS2013版本。
代码如下:
Private Sub bupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bupdate.Click
If Txtname.Text = "" Or Txtpassword.Text = "" Then
MsgBox("请首先在输入框中输入姓名及密码", MsgBoxStyle.Exclamation, "无法修改")
Exit Sub
End If
Dim curid As Integer
Dim curname As String
Dim curpass As String
curid = DataGrid1.Item(DataGrid1.CurrentRowIndex, 0)
curname = DataGrid1.Item(DataGrid1.CurrentRowIndex, 1)
curpass = DataGrid1.Item(DataGrid1.CurrentRowIndex, 2)
Dim infostring As String
infostring = "现在试图修改ID为" + curid.ToString + "的记录" + ControlChars.Cr
infostring += "姓名" + curname + ControlChars.Cr
infostring += "密码:" + curpass + ControlChars.Cr
infostring += "确定修改吗?"
Dim r As MsgBoxResult
r = MsgBox(infostring, MsgBoxStyle.YesNo, "确认修改")
If r = MsgBoxResult.No Then
Exit Sub
End If
Try
myds.Tables("login").Rows(DataGrid1.CurrentRowIndex).Item("name") = Txtname.Text 'myds(dataset).table获得name.text
myds.Tables("login").Rows(DataGrid1.CurrentRowIndex).Item("password") = Txtpassword.Text
Dim cmdstring As String
cmdstring = "update login"
cmdstring += "set name=?,password =?"
cmdstring += "where id=" + curid.ToString
oledbcmd.CommandText = cmdstring
oledbcmd.Parameters.Clear()
oledbcmd.Parameters.Add("pname", Txtname.Text)
oledbcmd.Parameters.Add("ppass", Txtpassword.Text)
oledbda.UpdateCommand = oledbcmd
oledbda.Update(myds, "login")
Catch ex As OleDb.OleDbException
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
------解决思路----------------------
set
where
前面都没有空格
参数应该用 @参数名 而不是问号