OLEdbcommand.Prepare错误

时间:2010-07-21 15:55:35

标签: .net vb.net ms-access

我收到错误:

OleDbCommand.Prepare方法要求所有参数都具有显式设置类型。

在下面代码的最后一行。 我看到有些事情要说你必须设置每个参数的数据类型,但是当命令构建器生成它时我该怎么做?

我看到有人说他们不喜欢使用自动生成的查询,这是不是一种糟糕的方式去做我想做的事情?

        Dim checkDock As New OleDbCommand("SELECT Instance, DocketNumber, FormID, FieldName, Occurrence, FieldValue  FROM fielddata WHERE docketnumber = @dock AND formid = @formid " & _
        "AND instance = @inst1")
        checkDock.Connection = New OleDbConnection("Provider=" & My.Settings.Provider & ";" & "Data Source=" & My.Settings.DataSource1)

        'create and execute the command to retrieve the saved data for the original instance
        checkDock.Parameters.AddWithValue("@dock", dock)
        checkDock.Parameters.AddWithValue("@formid", formid)
        checkDock.Parameters.AddWithValue("@inst1", inst1)

        Dim da As New OleDbDataAdapter(checkDock)

        Dim dSet As New DataSet("copySet")
        da.Fill(dSet)

        'create a command builder to put the new entries in the database
        Dim cb As New OleDb.OleDbCommandBuilder(da)

        Dim rowList As New ArrayList()

        For Each r As DataRow In dSet.Tables(0).Rows

            Dim dRow As DataRow = dSet.Tables(0).NewRow

            dRow.Item("Docketnumber") = dock2
            dRow.Item("Formid") = formid
            dRow.Item("fieldname") = r.Item("fieldname")
            dRow.Item("occurrence") = r.Item("occurrence")
            dRow.Item("fieldvalue") = r.Item("fieldvalue")
            dRow.Item("Instance") = inst2

            rowList.Add(dRow)
        Next

        For Each a As DataRow In rowList
            dSet.Tables(0).Rows.Add(a)
        Next

        da.Update(dSet)

1 个答案:

答案 0 :(得分:0)

您需要在此处指定参数类型。而不是这个:

checkDock.Parameters.AddWithValue("@dock", dock)
checkDock.Parameters.AddWithValue("@formid", formid)
checkDock.Parameters.AddWithValue("@inst1", inst1)

使用此方法:

// replace ??? with the correct type
checkDock.Parameters.Add("@dock", OleDbType.???).Value = dock;
checkDock.Parameters.Add("@formid", OleDbType.???).Value = formid;
checkDock.Parameters.Add("@inst1", OleDbType.???).Value = inst1;

希望有所帮助。