查询表达式vb.net访问中出现语法错误(缺少运算符)

时间:2018-06-21 14:14:53

标签: database vb.net ms-access

在尝试将一些信息添加到我的访问数据库时,我一直坚持这种编码方式

这是我的编码:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If TextBox1.Text & TextBox2.Text & TextBox3.Text & TextBox4.Text & TextBox5.Text & TextBox6.Text & TextBox7.Text = Nothing Then
            MsgBox("Please fill up all information.")
            TextBox1.Clear()
            TextBox2.Clear()
            TextBox3.Clear()
            TextBox4.Clear()
            TextBox5.Clear()
            TextBox6.Clear()
            TextBox7.Clear()
        Else
            Try
                Dim myConnection As OleDbConnection
                myConnection = New OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;Data Source=G:\Sem7 (Final Project)\Bookstore POS System\possys.mdb;Persist Security Info=false;")
                Dim myCommand As OleDbCommand
                myCommand = New OleDbCommand("INSERT INTO customer([ID],[c_name],[email],[phone],[nric],[sex],[race]) VALUES ('" & TextBox1.Text & "','" & TextBox2.Text & "'','" & TextBox3.Text & "'','" & TextBox4.Text & "'','" & TextBox5.Text & "'','" & TextBox6.Text & "'','" & TextBox7.Text & "')", myConnection)
                Dim id As New OleDbParameter("@ID", OleDbType.VarChar)
                Dim name As New OleDbParameter("@c_name", OleDbType.VarChar)
                Dim email As New OleDbParameter("@email", OleDbType.VarChar)
                Dim phone As New OleDbParameter("@phone", OleDbType.VarChar)
                Dim nric As New OleDbParameter("@nric", OleDbType.VarChar)
                Dim sex As New OleDbParameter("@sex", OleDbType.VarChar)
                Dim race As New OleDbParameter("@race", OleDbType.VarChar)
                id.Value = TextBox1.Text
                name.Value = TextBox2.Text
                email.Value = TextBox3.Text
                phone.Value = TextBox4.Text
                nric.Value = TextBox5.Text
                sex.Value = TextBox6.Text
                race.Value = TextBox7.Text
                myCommand.Parameters.Add(id)
                myCommand.Parameters.Add(name)
                myCommand.Parameters.Add(email)
                myCommand.Parameters.Add(phone)
                myCommand.Parameters.Add(nric)
                myCommand.Parameters.Add(sex)
                myCommand.Parameters.Add(race)
                myCommand.Connection.Open()
                myCommand.ExecuteNonQuery()
                myCommand.Dispose()
                MsgBox("Account Created", MsgBoxStyle.Information, "Registered!")
                myConnection.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If
    End Sub

1 个答案:

答案 0 :(得分:0)

  1. 您的验证码错误。 &符是vb.net中的可保留字符。这意味着它用于将小的字符串组合成更长的字符串。您已经为所有文本框构建了一个长字符串,并将其与Nothing进行了比较。如果用户仅填写一个框,则您的If为假。
  2. 即使您将If的行为更改为希望的行为,为什么还要清除所有文本框以使他们不得不重新输入内容,以惩罚用户呢?
  3. 验证代码属于每个文本框的Validate事件,因此让我们完全省去If语句。
  4. using语句可确保即使发生错误也可以关闭和处置对象。
  5. 我想知道ID字段是否为自动递增字段。如果是这样,则不应将其包含在Insert语句中。删除第一个参数和一个问号。如果不是自动递增,那么它是数字字段吗?如果是,则调整ID参数的数据类型,并将适当的转换添加到TextBox1.Text。
  6. 在Access(OleDB)中,将参数添加到参数集合的顺序不是重要的参数名称。
Private Sub CreateAccount()
        Try
            Using myConnection As New OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;Data Source=G:\Sem7 (Final Project)\Bookstore POS System\possys.mdb;Persist Security Info=false;")

                Using myCommand As New OleDbCommand("INSERT INTO customer([ID],[c_name],[email],[phone],[nric],[sex],[race]) VALUES (?,?,?,?,?,?,?)", myConnection)

                    myCommand.Parameters.Add("@ID", OleDbType.VarChar).Value = TextBox1.Text
                    myCommand.Parameters.Add("@c_name", OleDbType.VarChar).Value = TextBox2.Text
                    myCommand.Parameters.Add("@email", OleDbType.VarChar).Value = TextBox3.Text
                    myCommand.Parameters.Add("@phone", OleDbType.VarChar).Value = TextBox4.Text
                    myCommand.Parameters.Add("@nric", OleDbType.VarChar).Value = TextBox5.Text
                    myCommand.Parameters.Add("@sex", OleDbType.VarChar).Value = TextBox6.Text
                    myCommand.Parameters.Add("@race", OleDbType.VarChar).Value = TextBox7.Text

                    myConnection.Open()
                    myCommand.ExecuteNonQuery()
                    myConnection.Close()
                    MsgBox("Account Created", MsgBoxStyle.Information, "Registered!")

                End Using
            End Using
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub