VB.NET / .NET 4.8:插入到语句中的语法错误...如果遗漏,还有用户 ID 不断告诉我缺少值 VISUAL STUDIO 2022

问题描述 投票:0回答:1
Imports System.Data.OleDb

Public Class MainForm

Dim conn As New OleDbConnection
Dim cmd As OleDbCommand
Dim myread As OleDbCommand
Dim dt As New DataTable
Dim da As New OleDbDataAdapter(cmd)
Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    conn.ConnectionString =          "Provider=Microsoft.ACE.OLEDB.12.0;DataSource=|DataDirectory|\ISIOLO.accdb"

End Sub

Private Sub BtnCreate_Click(sender As Object, e As EventArgs) Handles BtnCreate.Click
    conn.Open()
    cmd = conn.CreateCommand()
    cmd.CommandType = CommandType.Text

    cmd.CommandText = "INSERT INTO USERS(EMAIL,PASSWORD,DESIGNATION)     VALUES(@Email,@Password,@Designation)"

    cmd.Parameters.AddWithValue("@Email", TxtEmail.Text)
    cmd.Parameters.AddWithValue("@Password", TxtPassword.Text)
    cmd.Parameters.AddWithValue("@Designation", ComboDesignation.Text)

    Try
        cmd.ExecuteNonQuery()
        Console.WriteLine(cmd.CommandText)
        For Each parameter As OleDbParameter In cmd.Parameters
            Console.WriteLine($"{parameter.ParameterName}: {parameter.Value}")
        Next
        conn.Close()
        MessageBox.Show("successful")
        PnlCreateAccount.Visible = False


    Catch ex As Exception

        MessageBox.Show(ex.Message)
        conn.Close()

    End Try
 End Sub
 End Class

将用户添加到访问表中 - 它有四列:

USERID
EMAIL TEXT
PASSWORD  TEXT
DESIGNATION  TEXT

USER ID
是自动递增的,似乎不能被遗漏

vb.net ms-access-2010 ms-access-2013
1个回答
0
投票

“Password”是 Jet/ACE SQL 中的保留字,因此在将其用作标识符时需要对其进行转义:

cmd.CommandText = "INSERT INTO USERS (EMAIL, [PASSWORD], DESIGNATION) VALUES (@Email, @Password, @Designation)"

理想情况下,您不应该将密码直接存储在数据库中,而是对它们进行哈希处理。然后,您可以合理地命名您的列

PasswordHash
并完全避免该问题。

© www.soinside.com 2019 - 2024. All rights reserved.