标题:[求助]!!!运行不了
只看楼主
kenny_910
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2005-11-30
 问题点数:0 回复次数:1 
[求助]!!!运行不了

请大家帮帮忙,帮我看下面的代码一(功能主要是想实现新增用户)错在哪里?运行时提示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

搜索更多相关主题的帖子: 运行 
2005-12-01 09:10
龙舟
Rank: 1
等 级:新手上路
帖 子:46
专家分:0
注 册:2005-11-5
得分:0 

我一般添加数据的时候这样写:

Dim mySelectQuery As String = "INSERT INTO admintb (字段1,字段2,字段3,字段4) VALUES ('123','234','345','1')"
Dim myConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\MyProj\database.mdb")
Dim mycommand As New OleDbCommand(mySelectQuery, myConnection)
myConnection.Open()

mycommand.ExecuteNonQuery()
myConnection.Close() '添加数据

2005-12-01 12:33



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-36122-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.292149 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved