Insert text from a text box into and Access 2010 DataBase using VB.Net

后端 未结 1 1750
生来不讨喜
生来不讨喜 2021-01-24 14:36

I have 3 text boxes I am using as a contact form. I am trying to use VB to take this data and add it into my database. I have run debugging and it says the error is in the INSER

相关标签:
1条回答
  • 2021-01-24 14:49

    USER is a reserved keyword in MS-Access, To use it as a table name your need to enclose it in square brackets

    strSql = "INSERT INTO [user] (username, email, comments) VALUES (?,?,?)"
    

    As a side note, try to use the Using Statement when you work with disposable objects.

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim Name As String = txtName.Text
        Dim Email As String = tbxEmail.Text
        Dim Comment As String = tbxComment.Text
    
        Using dbConn = New OleDbConnection(".......")
            dbConn.Open()
            Dim strSql = "INSERT INTO [user] (username, email, comments) VALUES (?,?,?)"
            Using objcmd = New OleDbCommand(strSql, dbConn)
                objcmd.Parameters.AddWithValue("@username", Name)
                objcmd.Parameters.AddWithValue("@email", Email)
                objcmd.Parameters.AddWithValue("@comments", Comment)
                objcmd.ExecuteNonQuery()
            End Using
        End Using
        Response.Write("Submitted Successfully")
    End Sub
    
    0 讨论(0)
提交回复
热议问题