无法将数据保存到我的Access 2007数据库中

时间:2019-06-01 17:04:43

标签: vb.net

我无法在Access 2007中保存数据。当我尝试单击“保存”按钮时,收到消息

  

插入到语句中的语法错误。

我的代码有什么错误?

Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click
    If Len(Trim(TxtUserid.Text)) = 0 Then
        MessageBox.Show("Please enter Student No.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        TxtUserid.Focus()
        Exit Sub
    End If

    If Len(Trim(TxtUsername.Text)) = 0 Then
        MessageBox.Show("Please enter Username", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        TxtUsername.Focus()
        Exit Sub
    End If
    If Len(Trim(txtreenterpassword.Text)) = 0 Then
        MessageBox.Show("Please enter Password", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        txtreenterpassword.Focus()
        Exit Sub
    End If

    Try
        con = New OleDbConnection(cs)
        con.Open()
        Dim ct As String = "select Userid from Users where Userid=@find"

        cmd = New OleDbCommand(ct)
        cmd.Connection = con
        cmd.Parameters.Add(New OleDbParameter("@find", System.Data.OleDb.OleDbType.VarChar, 20, "Userid"))
        cmd.Parameters("@find").Value = TxtUserid.Text
        rdr = cmd.ExecuteReader()

        If rdr.Read Then
            MessageBox.Show("Student No. Already Exists", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

            If Not rdr Is Nothing Then
                rdr.Close()
            End If
        Else
            con = New OleDbConnection(cs)
            con.Open()

            Dim ab As String = "insert into Users(Userid,USERNAME,USERTYPE,PASSWORD,FIRSTNAME,MI,LASTNAME,CONTACT,BIRTHDAY,Age) VALUES (@a1,@a2,@a3,@a4,@a5,@a6,@a7,@a8,@a9,@a10)"

            cmd = New OleDbCommand(ab)
            cmd.Connection = con
            cmd.Parameters.Add(New OleDbParameter("@a1", System.Data.OleDb.OleDbType.VarChar, 20, "Userid"))
            cmd.Parameters.Add(New OleDbParameter("@a2", System.Data.OleDb.OleDbType.VarChar, 100, "USERNAME"))
            cmd.Parameters.Add(New OleDbParameter("@a3", System.Data.OleDb.OleDbType.VarChar, 250, "USERTYPE"))
            cmd.Parameters.Add(New OleDbParameter("@a4", System.Data.OleDb.OleDbType.VarChar, 50, "PASSWORD"))
            cmd.Parameters.Add(New OleDbParameter("@a5", System.Data.OleDb.OleDbType.VarChar, 200, "FIRSTNAME"))
            cmd.Parameters.Add(New OleDbParameter("@a6", System.Data.OleDb.OleDbType.VarChar, 150, "MI"))
            cmd.Parameters.Add(New OleDbParameter("@a7", System.Data.OleDb.OleDbType.VarChar, 15, "LASTNAME"))
            cmd.Parameters.Add(New OleDbParameter("@a8", System.Data.OleDb.OleDbType.VarChar, 250, "CONTACT"))
            cmd.Parameters.Add(New OleDbParameter("@a9", System.Data.OleDb.OleDbType.VarChar, 15, "BIRTHDAY"))
            cmd.Parameters.Add(New OleDbParameter("@a10", System.Data.OleDb.OleDbType.VarChar, 250, "Age"))

            cmd.Parameters("@a1").Value = TxtUserid.Text
            cmd.Parameters("@a2").Value = TxtUsername.Text
            cmd.Parameters("@a3").Value = Cmbusertype.Text
            cmd.Parameters("@a4").Value = txtreenterpassword.Text
            cmd.Parameters("@a5").Value = TxtFirstname.Text
            cmd.Parameters("@a6").Value = txtMi.Text
            cmd.Parameters("@a7").Value = TxtLastname.Text
            cmd.Parameters("@a8").Value = TxtContact.Text
            cmd.Parameters("@a9").Value = DateTimePicker3.Text
            cmd.Parameters("@a10").Value = TxtAge.Text

            cmd.ExecuteReader()

            MessageBox.Show("Successfully Saved", "Issued Details", MessageBoxButtons.OK, MessageBoxIcon.Information)

            If con.State = ConnectionState.Open Then
                con.Close()
            End If
            con.Close()
        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        con.Dispose()
    End Try
End Sub

2 个答案:

答案 0 :(得分:0)

您为什么使用:

cmd.ExecuteReader()

您可以尝试使用:

cmd.ExecuteNonQuery()

答案 1 :(得分:0)

稍微移动了一些东西。代码注释。

Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click
    'don't do everything is one huge procedure
    'this procedure is about saving data
    'Off load Validation code to a separate function
    If Not ValidInput() Then
        Exit Sub
    End If
    If Not UniqueUserID() Then
        Exit Sub
    End If

    Using con As New OleDbConnection(cs)
        'Notic the brackest surrounding PASSWORD
        Dim ab As String = "insert into Users(Userid,USERNAME,USERTYPE,[PASSWORD],FIRSTNAME,MI,LASTNAME,CONTACT,BIRTHDAY,Age) VALUES (@a1,@a2,@a3,@a4,@a5,@a6,@a7,@a8,@a9,@a10)"
        Using cmd As New OleDbCommand(ab, con)
            cmd.Connection = con
            'Excellent way to add parameters with the type and size
            cmd.Parameters.Add("@a1", System.Data.OleDb.OleDbType.VarChar, 20, "Userid").Value = TxtUserid.Text
            cmd.Parameters.Add("@a2", System.Data.OleDb.OleDbType.VarChar, 100, "USERNAME").Value = TxtUsername.Text
            cmd.Parameters.Add("@a3", System.Data.OleDb.OleDbType.VarChar, 250, "USERTYPE").Value = Cmbusertype.Text
            cmd.Parameters.Add("@a4", System.Data.OleDb.OleDbType.VarChar, 50, "[PASSWORD]").Value = txtreenterpassword.Text
            cmd.Parameters.Add("@a5", System.Data.OleDb.OleDbType.VarChar, 200, "FIRSTNAME").Value = TxtFirstname.Text
            cmd.Parameters.Add("@a6", System.Data.OleDb.OleDbType.VarChar, 150, "MI").Value = txtMi.Text
            cmd.Parameters.Add("@a7", System.Data.OleDb.OleDbType.VarChar, 15, "LASTNAME").Value = TxtLastname.Text
            cmd.Parameters.Add("@a8", System.Data.OleDb.OleDbType.VarChar, 250, "CONTACT").Value = TxtContact.Text
            cmd.Parameters.Add("@a9", System.Data.OleDb.OleDbType.VarChar, 15, "BIRTHDAY").Value = DateTimePicker3.Text
            cmd.Parameters.Add("@a10", System.Data.OleDb.OleDbType.VarChar, 250, "Age").Value = TxtAge.Text
            'Don't open the connection until right before it is used
            con.Open()
            cmd.ExecuteNonQuery()
        End Using
    End Using
    MessageBox.Show("Successfully Saved", "Issued Details", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub

Private Function ValidInput() As Boolean
    If Len(Trim(TxtUserid.Text)) = 0 Then
        MessageBox.Show("Please enter Student No.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        TxtUserid.Focus()
        Return False
    End If
    If Len(Trim(TxtUsername.Text)) = 0 Then
        MessageBox.Show("Please enter Username", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        TxtUsername.Focus()
        Return False
    End If
    If Len(Trim(txtreenterpassword.Text)) = 0 Then
        MessageBox.Show("Please enter Password", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        txtreenterpassword.Focus()
        Return False
    End If
    Return True
End Function

Private Function UniqueUserID() As Boolean
    Dim ReturnValue As Object 'We are using Object because it might have to hold a value of Nothing
    'Keep your database objects local so you can be sure they are closed and disposed
    'Using...End Using blocks to the for you
    Using con As New OleDbConnection(cs)
        'You can pass the command text and the connection directly to the constructor of the command
        Using cmd As New OleDbCommand("select 1 from Users where Userid=@find", con)
            'The add method will create the Parameter for you. No need for New Parameter
            'You can also add the .Value property directly in this line
            cmd.Parameters.Add("@find", System.Data.OleDb.OleDbType.VarChar, 20, "Userid").Value = TxtUserid.Text
            con.Open()
            ReturnValue = cmd.ExecuteScalar() 'Retrieves the first column of the first row in the resultset
        End Using
    End Using
    If ReturnValue Is Nothing Then
        'insert the record
        Return True
    ElseIf CInt(ReturnValue) = 1 Then
        MessageBox.Show("Student No. Already Exists", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Return False
    Else
        Return False
    End If
End Function

请注意,将年龄存储在数据库中不是一个好主意。第二天可能会改变!只需根据需要存储生日和计算出的年龄即可。

另一个说明;您可能要检查DateTimePicker返回的文本。我相信它会因设置而异。 Short似乎很好用。