VB形式INSERT INTO说法,更改密码表单
嘿,我工作的一个学校项目,并遇到了一些问题VB形式INSERT INTO说法,更改密码表单
我试图做出改变密码表单。我有一个名为“tblLogin”的访问表,其中有用户名,密码,SecQuest,SecAns,Level。
用户名是属于我的登录表单
该方案目前未在tblLogin表更改密码值的公共变量
我以为这是我的INSERT问题INTO声明
非常感谢您提供任何帮助!
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim sql As String
Dim Conn As New OleDb.OleDbConnection("PROVIDER = Microsoft.ACE.OLEDB.12.0;Data Source = C:\Users\Nelson\Documents\SystemDB\DB.accdb")
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim response As Integer
Try
Conn.Open()
'establishes communication with the database
sql = "SELECT Password FROM tblLogin WHERE Username = """ & LoginForm.username & """"
'declares SQL statement
da = New OleDb.OleDbDataAdapter(sql, Conn)
'runs sql statement on database
da.Fill(ds, "passwordcheck")
'fills in a dataset called "passwordcheck" with infomation selected by the sql statement
If TextBox1.Text = TextBox2.Text And TextBox3.Text = TextBox4.Text And TextBox1.Text = ds.Tables("passwordcheck").Rows(0).Item(0) And TextBox1.Text <> "" And TextBox2.Text <> "" And TextBox3.Text <> "" And TextBox4.Text <> "" Then
response = MessageBox.Show("Are you sure?", "Attention", MessageBoxButtons.YesNo)
If response = MsgBoxResult.Yes Then
Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("INSERT INTO tblLogin Password VALUES (?) WHERE Username = """ & LoginForm.username & """", Conn)
cmd.Parameters.Add("@Password", OleDb.OleDbType.VarChar).Value = TextBox3.Text
Conn.Close()
End If
Else
MessageBox.Show("Passwords entered are incorrect", "Attention")
End If
Catch ex As Exception
MessageBox.Show("Passwords entered are incorrect", "Attention")
End Try
End Sub
回答:
PASSWORD是MS-Access的保留关键字。如果您想将其用于列名,请将其放在sqare括号内。说,请为您的命令使用参数化查询。
Conn.Open() 'establishes communication with the database
sql = "SELECT [Password] FROM tblLogin WHERE Username = ?"
'declares SQL statement
da = New OleDb.OleDbDataAdapter(sql, Conn)
da.SelectCommand("@p1", LoginForm.username)
.....
最后的INSERT子句不需要一个WHERE语句 (这是没有意义的设置条件来选择一个插入一些行)
Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("INSERT INTO tblLogin [Password] " & _ "VALUES (?) "
但看你的代码,那么很可能你需要更新,而不是一个INSERT
Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("UPDATE tblLogin " & _ "SET [Password] = ? WHERE Username ?"
cmd.Parameters.Add("@Password", OleDb.OleDbType.VarChar).Value = TextBox3.Text
cmd.Parameters.Add("@Username", OleDb.OleDbType.VarChar).Value = TEXTBOXFORUSERNAME.Text
---和最重要的线---
cmd.ExecuteNonQuery()
没有这个没有尝试更新数据库表
以上是 VB形式INSERT INTO说法,更改密码表单 的全部内容, 来源链接: utcz.com/qa/260875.html