国务卿女士第三季剧情:vb+sql插入新记录的问题

来源:百度文库 编辑:神马品牌网 时间:2024/04/28 17:10:03
使用ADO连接

Dim dp As New Connection
With dp
.Provider = "SQLOLEDB"
.ConnectionString = "User ID=sa;PWD=;Data Source=(local);" & _
"Initial Catalog=tushu"
.Open
End With
Set ts = New Recordset

With ts
.CursorLocation = adUseClient '指定使用客户端光标
.CursorType = adOpenStatic '指定使用静态光标
.Open "SELECT * FROM 登录资料", dp, adOpenDynamic, adCmdText

Set .ActiveConnection = Nothing
End With
Dim h, b, c As String
h = Text1.Text
b = Text2.Text
c = Text3.Text
Dim cmd As New Command
Set cmd.ActiveConnection = dp
cmd.CommandText = "INSERT INTO 登录资料(用户编号, 用户名, 密码) VALUES (h, b, c)"

cmd.CommandType = adCmdText
cmd.Execute

如何插入text中的文本,直接在insert中插字符串可以,为什么用变量就不行?

你需要修改Command对象的CommandText属性,修改成这样就可以了:
cmd.CommandText="INSERT INTO 登录资料(用户编号, 用户名, 密码) VALUES (" + h + "," + b + "," + c + ")"

因为你的变量名在字符串里面,被认为是字符串的一部分,用加号连接字符串与变量表示把字符串与变量的值连接起来。
建议你在执行命令之前先检查用户输入数据的合法性,避免注入漏洞。