Access 2010 的 INSERT INTO 语句中的语法错误 [英] Syntax error in INSERT INTO statement for Access 2010

查看:38
本文介绍了Access 2010 的 INSERT INTO 语句中的语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 INSERT 语句显然有语法错误.有人可以解释一下为什么会这样吗?

My INSERT statement apparently has a syntax error. Could someone please explain why that might be?

Private Sub Register_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Register.Click
    Dim StudentNum As String
    Dim Password As String
    Dim FirstName As String
    Dim LastName As String
    Dim YrandSec As String

    StudentNum = Number.Text()
    Password = Pass.Text
    FirstName = First.Text
    LastName = Last.Text
    YrandSec = YrSec.Text()

    SQL = "INSERT INTO Accounts(StudNo,Password,FirstName,LastName,YrandSec) VALUES ('" & StudentNum & "', '" & Password & "', '" & FirstName & "', '" & LastName & "', '" & YrandSec & "')"    - ERROR HERE
    Cmd = New OleDbCommand(SQL, Con)
    Con.Open()
    objCmd = New OleDbCommand(SQL, Con)

    If Repass.Text = Pass.Text = False Then
        Re.Text = "*Password didn't match!"
        Number.Text = ""
        Pass.Text = ""
        Repass.Text = ""
        Con.Close()
    Else
        If Number.Text = "" Or Pass.Text = "" Or Repass.Text = "" Or First.Text = "" Or Last.Text = "" Or YrSec.Text = "" Then
            MsgBox("Please complete the field", MsgBoxStyle.Information, "Failed to create")
        Else
            objCmd.ExecuteNonQuery()
            Re.Text = ""
            MsgBox("Account has been created", MsgBoxStyle.Information, "Congrats!")
            For fade = 0.0 To 1.1 Step 0.2
                Login.Opacity = fade
                Login.Show()
                Me.Hide()
                Threading.Thread.Sleep(30)
                Number.Text = ""
                Pass.Text = ""
                Repass.Text = ""
                First.Text = ""
                Last.Text = ""
                YrSec.Text = ""
            Next
        End If

    End If
End Sub

推荐答案

  1. PASSWORD 是 Access SQL 中的保留字,因此您需要将该列名称括在方括号中.

  1. PASSWORD is a reserved word in Access SQL, so you need to wrap that column name in square brackets.

您确实应该使用参数化查询来防止SQL 注入,并且通常会让您的生活更轻松.

You really should use a parameterized query to protect against SQL Injection and generally make your life easier.

尝试这样的事情

SQL = "INSERT INTO [Accounts] ([StudNo],[Password],[FirstName],[LastName],[YrandSec]) " & _
        "VALUES (?, ?, ?, ?, ?)"
Con.Open()
objCmd = New OleDbCommand(SQL, Con)
objCmd.Parameters.AddWithValue("?", StudentNum)
objCmd.Parameters.AddWithValue("?", Password)
objCmd.Parameters.AddWithValue("?", FirstName)
objCmd.Parameters.AddWithValue("?", LastName)
objCmd.Parameters.AddWithValue("?", YrandSec)

这篇关于Access 2010 的 INSERT INTO 语句中的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆