VB 表单 INSERT INTO 语句,更改密码表单

Posted

技术标签:

【中文标题】VB 表单 INSERT INTO 语句,更改密码表单【英文标题】:VB Forms INSERT INTO statement, Changing Password Form 【发布时间】:2014-02-15 16:18:05 【问题描述】:

嘿,我正在做一个学校项目,遇到了一些问题

我正在尝试制作更改密码表单。我有一个名为“tblLogin”的访问表,其中包含用户名、密码、SecQuest、SecAns、级别等列。

用户名是一个公共变量,属于我的登录表单

程序目前没有改变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

【问题讨论】:

【参考方案1】:

PASSWORD 是 MS-Access 的保留关键字。如果要将其用于列名,请用方括号括起来。也就是说,请为您的命令使用参数化查询。

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 语句 (为 INSERT 选择某些行设置条件是没有意义的)

Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("INSERT INTO tblLogin [Password] " & _
                                "VALUES (?) "

但是看看你的代码,你可能需要一个 UPDATE 而不是一个 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()

没有这个就不会尝试更新数据库表

【讨论】:

Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("UPDATE tblLogin" & "SET [Password] = ? WHERE Username ?", Conn) cmd.Parameters.Add("@Password", OleDb.OleDbType.VarChar).Value = TextBox3.Text cmd.Parameters.Add("@Username", OleDb.OleDbType.VarChar).Value = LoginForm.username Conn.Close() 这个不行,密码值在tblLogin 没有改变,是我出错了吗?

以上是关于VB 表单 INSERT INTO 语句,更改密码表单的主要内容,如果未能解决你的问题,请参考以下文章

vb sql insert into 语句语法错误 求解

在 INSERT INTO 语句中插入值时出现语法错误。 -VB 和 ACCESS 2013

VB 操作sqlserver执行insert into语句,如何返回自增的ID值?

VB.Net OLEDB ExecuteNonQuery INSERT INTO - 对数据库没有影响

MS Access 的 SQL INSERT INTO 语句

“INSERT INTO 语句中的语法错误”错误 - 错误是啥?