0

我正在开发我的第一个数据库程序,但无法在 access 数据库中更新数据。这是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

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

4

2 回答 2

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 并将其参数设置为数据库表中预期的值。
您的列的数据类型也存在问题。从您的示例中无法推断数据类型,但类型、排名和金额可能是数字列,因此您需要将文本框值转换为适当的数字(Convert.ToInt32(textbox.text))

于 2013-03-11T18:54:19.177 回答
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()
于 2013-03-11T18:53:56.603 回答