请大家帮帮忙,帮我看下面的代码一(功能主要是想实现新增用户)错在哪里?运行时提示insert into语法错误.但是我改成下面的代码二,运行就没问题
代码一
Try
Dim insertCmd, selectCmd, connStr As String
connStr = "provider=microsoft.jet.oledb.4.0;data source=D:\资料\study\VB.NET\学习\人事信息系统\person.mdb"
insertCmd = "insert into users(user_name,password) values(@用户名,@密码)"
selectCmd = "select * from users"
Dim conn As OleDbConnection, myAdapter As OleDbDataAdapter, cmd As OleDbCommand
Dim myDataset As DataSet
myDataset = New DataSet
conn = New OleDbConnection(connStr)
conn.Open()
myAdapter = New OleDbDataAdapter(selectCmd, conn)
myDataset = New DataSet
myAdapter.Fill(myDataset, "users")
cmd = New OleDbCommand(insertCmd, conn)
cmd.Parameters.Add(New OleDbParameter("@用户名", OleDbType.Char))
cmd.Parameters.Add(New OleDbParameter("@密码", OleDbType.Char))
cmd.Parameters("@用户名").Value = txtUserName.Text
cmd.Parameters("@密码").Value = Trim(txtPassword.Text)
cmd.ExecuteNonQuery()
myAdapter.Update(myDataset, "users")
MsgBox("添加用户成功", MsgBoxStyle.OKOnly, "信息框")
txtUserName.Text = ""
txtPassword.Text = ""
txtOKpassword.Text = ""
txtUserName.Focus()
conn.Close()
Catch
MsgBox(Err.Description)
End Try
代码二
Try
Dim insertCmd, selectCmd, connStr As String
connStr = "provider=microsoft.jet.oledb.4.0;data source=D:\资料\study\VB.NET\学习\人事信息系统\person.mdb"
insertCmd = "insert into users(user_name) values(@用户名)"
selectCmd = "select * from users"
Dim conn As OleDbConnection, myAdapter As OleDbDataAdapter, cmd As OleDbCommand
Dim myDataset As DataSet
myDataset = New DataSet
conn = New OleDbConnection(connStr)
conn.Open()
myAdapter = New OleDbDataAdapter(selectCmd, conn)
myDataset = New DataSet
myAdapter.Fill(myDataset, "users")
cmd = New OleDbCommand(insertCmd, conn)
cmd.Parameters.Add(New OleDbParameter("@用户名", OleDbType.Char))
cmd.Parameters("@用户名").Value = txtUserName.Text
cmd.ExecuteNonQuery()
myAdapter.Update(myDataset, "users")
MsgBox("添加用户成功", MsgBoxStyle.OKOnly, "信息框")
txtUserName.Text = ""
txtPassword.Text = ""
txtOKpassword.Text = ""
txtUserName.Focus()
conn.Close()
Catch
MsgBox(Err.Description)
End Try