将数据插入oleDb表

时间:2013-03-11 18:46:56

标签: database vb.net oledb

我正在处理我的第一个数据库程序,我无法在访问数据库中更新数据。以下是winform上“添加条件”按钮的代码。

 Dim TaxConnStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ConfigurationManager.AppSettings("Database")
    Dim dbConnection As OleDbConnection = New OleDbConnection(TaxConnStr)
    Dim dt As New DataTable
    Dim da As New OleDbDataAdapter

    Try
        dbConnection.Open()

        Dim cmd As New OleDbCommand
        cmd.CommandText = "INSERT INTO UserCriteria ( UserName, State, County, Type, Amount, Rank) VALUES ( @UserName, @State, @County, @Type, @Amount, @Rank)"
        Me.DataGridView1.DataSource = dt
        cmd.ExecuteNonQuery()

        dt.Columns.Add("UserName")
        dt.Columns.Add("State")
        dt.Columns.Add("County")
        dt.Columns.Add("Type")
        dt.Columns.Add("Amount")
        dt.Columns.Add("Rank")
        dt.Rows.Add(New String() {
                                            boxAssignTo.Text, _
                                            boxState.Text, _
                                            boxCounty.Text, _
                                            boxAmount.Text, _
                                            boxType.Text, _
                                            boxRank.Text})





    Catch ex As Exception
    Finally
        dbConnection.Close()
    End Try

我知道我错过了什么或做错了,所以我要求你上师的指导。

2 个答案:

答案 0 :(得分:1)

您的代码中存在一些错误。

试试这个

Dim TaxConnStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & _
                            ConfigurationManager.AppSettings("Database")
Using dbConnection = New OleDbConnection(TaxConnStr)
    Dim dt As New DataTable
    Dim da As New OleDbDataAdapter
    Try
        dbConnection.Open()
        Dim cmd As New OleDbCommand
        cmd.CommandText = "INSERT INTO UserCriteria ( UserName, State, County, " & _
                          "Type, Amount, Rank) VALUES " & _
                          "(@UserName, @State, @County, @Type, @Amount, @Rank)"
       cmd.Parameters.AddWithValue("@UserName", boxAssignTo.Text)
       cmd.Parameters.AddWithValue("@State", boxState.Text)
       cmd.Parameters.AddWithValue("@County", boxCounty.Text)
       cmd.Parameters.AddWithValue("@Type", boxType.Text)
       cmd.Parameters.AddWithValue("@Amount", boxAmount.Text)
       cmd.Parameters.AddWithValue("@Rank", boxRank.Text)
       cmd.ExecuteNonQuery()
   Catch ex As Exception
   Finally
       dbConnection.Close()
   End Try
End Using

正如您所看到的,在数据​​库表中添加记录时,可以使用带命令文本的OleDbCommand,并将其参数设置为数据库表中预期的值。
您的列的数据类型也存在问题。从您的示例中,无法推断数据类型,但可能Type,Rank和Amount是数字列,因此您需要将文本框值转换为适当的数字(Convert.ToInt32(textbox.text))

答案 1 :(得分:0)

看起来你在那里组合了两个概念/对象。您可以使用OleDbCommand插入记录:

Dim cmd As New OleDbCommand
cmd.CommandText = "INSERT INTO UserCriteria ( UserName, State, County, Type, Amount, Rank) VALUES ( @UserName, @State, @County, @Type, @Amount, @Rank)"
cmd.Parameters.Add(new OleDbParameter("@UserName", DbType.String))
cmd.Parameters.Add(new OleDbParameter("@State", DbType.String))
cmd.Parameters.Add(new OleDbParameter("@County", DbType.String))
cmd.Parameters.Add(new OleDbParameter("@Type", DbType.String))       ' DbType depends on column type in database
cmd.Parameters.Add(new OleDbParameter("@Amount", DbType.Integer))    ' DbType depends on column type in database
cmd.Parameters.Add(new OleDbParameter("@Rank", DbType.Integer))      ' DbType depends on column type in database

cmd.Prepare();   

cmd.Parameters("@UserName").Value = boxAssignTo.Text
cmd.Parameters("@State").Value = boxState.Text
cmd.Parameters("@County").Value = boxCounty.Text
cmd.Parameters("@Type").Value = boxType.Text
cmd.Parameters("@Amount").Value = boxAmount.Text
cmd.Parameters("@Rank").Value = boxRank.Text

cmd.ExecuteNonQuery()