为什么我的值没有插入Access数据库?

时间:2015-12-18 12:03:34

标签: vb.net sql-insert

虽然代码没有显示任何错误,但当我去检查数据库中的表时,这些值不会出现。

 If Feedbacktxt.Text = "" Then
      MsgBox("You cannot submit empty feedback")
 Else

     provider = "Provider = Microsoft.ACE.OLEDB.12.0;Data Source="      'Telling your program what data source we're using

    datafile = "D:\HistoryDatabase\HistoryDatabase.accdb" 'The location of the datafile to where 

    connectstring = provider & datafile

    myconnection.ConnectionString = connectstring 'An object that allows us to connect to the microsoft connection 

    myconnection.Open() 'Opens up the database file

    Dim SQLInsert As String = "INSERT INTO AccountNames([TeacherFeedBack]) VALUES ('" & Feedbacktxt.Text & "') where [Username] = " & NameComboBox.SelectedItem
    Dim cmd2 As OleDbCommand = New OleDbCommand(SQLInsert, myconnection)
    Feedbacktxt.Clear()
    MsgBox("Your feedback has been submitted")

End If

2 个答案:

答案 0 :(得分:0)

您的SQL中有错误,在这种情况下,将数据插入数据库时​​不需要WHERE子句。

Dim SQLInsert As String = "INSERT INTO AccountNames([TeacherFeedBack]) VALUES ('" & Feedbacktxt.Text & "')"

尝试修复该行,或进一步解释。

答案 1 :(得分:0)

第一个示例和第二个示例的不同之处在于创建命令文本

使用xml文字

Public Function AddNewAccount(ByVal TeacherFeedBack As String) As Integer
    Dim newPrimaryKey As Integer = 0

    ' easily create connection string w/o string concatenation
    Dim Builder As New OleDb.OleDbConnectionStringBuilder With
        {
            .Provider = "Microsoft.ACE.OLEDB.12.0",
            .DataSource = "D:\HistoryDatabase\HistoryDatabase.accdb"
        }

    Using cn As New OleDb.OleDbConnection With
        {
            .ConnectionString = Builder.ConnectionString
        }
        Using cmd As New OleDb.OleDbCommand With {.Connection = cn}
            ' use xml literal for creating nicely formatted query
            ' use parameter which applies proper formatting for the
            ' value, in this case a string and if there are embedded
            ' apostrophe will properly escape them
            cmd.CommandText =
                <SQL>
                    INSERT INTO AccountNames 
                    (
                        TeacherFeedBack
                    ) 
                    Values
                    (
                        @TeacherFeedBack
                    )
                </SQL>.Value

            '
            ' Set value for above parameter. Some will say to use 
            ' ? rather than a named parameter but named parameters are
            ' great when using many parameters. Named parameters need
            ' to be in the same order as the elements in the values part
            ' of the query.
            '
            cmd.Parameters.AddWithValue("@TeacherFeedBack", TeacherFeedBack)
            cn.Open()
            ' execute insert query, check if one was actually added
            If cmd.ExecuteNonQuery() = 1 Then
                '
                ' Get new primary key for record just added above
                '
                cmd.CommandText = "Select @@Identity"
                newPrimaryKey = CInt(cmd.ExecuteScalar)
            End If
        End Using
    End Using

    Return newPrimaryKey

End Function

没有xml文字

Public Function AddNewAccount(ByVal TeacherFeedBack As String) As Integer
    Dim newPrimaryKey As Integer = 0

    ' easily create connection string w/o string concatenation
    Dim Builder As New OleDb.OleDbConnectionStringBuilder With
        {
            .Provider = "Microsoft.ACE.OLEDB.12.0",
            .DataSource = "D:\HistoryDatabase\HistoryDatabase.accdb"
        }

    Using cn As New OleDb.OleDbConnection With
        {
            .ConnectionString = Builder.ConnectionString
        }
        Using cmd As New OleDb.OleDbCommand With {.Connection = cn}
            ' use xml literal for creating nicely formatted query
            ' use parameter which applies proper formatting for the
            ' value, in this case a string and if there are embedded
            ' apostrophe will properly escape them
            cmd.CommandText = "INSERT INTO AccountNames (TeacherFeedBack) Values (@TeacherFeedBack)"

            '
            ' Set value for above parameter. Some will say to use 
            ' ? rather than a named parameter but named parameters are
            ' great when using many parameters. Named parameters need
            ' to be in the same order as the elements in the values part
            ' of the query.
            '
            cmd.Parameters.AddWithValue("@TeacherFeedBack", TeacherFeedBack)
            cn.Open()
            ' execute insert query, check if one was actually added
            If cmd.ExecuteNonQuery() = 1 Then
                '
                ' Get new primary key for record just added above
                '
                cmd.CommandText = "Select @@Identity"
                newPrimaryKey = CInt(cmd.ExecuteScalar)
            End If
        End Using
    End Using

    Return newPrimaryKey

End Function