0

我正在使用以下代码创建将我的 DataTable 内容保存到并清空 mdb 数据库,所以首先我创建了 datata 表,当我尝试插入数据时出现以下错误:没有为一个或多个参数提供值,现在我认为此错误是由使用此链接中提到的 Text 引起的

但我不知道如何修复它,这是代码,我在注释行中得到异常

Dim cmd = cnn.CreateCommand
                cmd.CommandText = "CREATE TABLE productstable(supplierid Integer ,catalogid Integer ,ccode Text ,cname Text,pother2 Text, pother3 Double, cprice Double, rank Integer, rankchange Integer , PD Double, PDP Double , TPD Double, TPDP Double, sprice Double, msprice Double, minprice Double, sopriceDB Double, pother4 Long, usedsuppliermargin Double, cimageurl Text, ccategory Integer, pricetaggroup Byte , spricemargingroup Byte)"
                cmd.ExecuteNonQuery()

                Dim ocmd As New OleDbCommand("INSERT INTO productstable(supplierid , catalogid, ccode,cname,pother2,pother3,cprice,rank,rankchange,PD,PDP,TPD,TPDP,sprice,msprice,minprice,sopriceDB,pother4,usedsuppliermargin,cimageurl,ccategory,pricetaggroup,spricemargingroup) VALUES (@supplierid , @catalogid, @ccode, @cname, @pother2, @pother3, @cprice, @rank, @rankchange, @PD, @PDP, @TPD, @TPDP, @sprice, @msprice, @minprice, @sopriceDB, @pother4, @usedsuppliermargin,@cimageurl,@ccategory,@pricetaggroup,@spricemargingroup)", cnn)

                For i = 0 To mainDatatable.Rows.Count - 1
                    ocmd.Parameters.Add(New OleDbParameter("@supplierid", mainDatatable.Rows(i)("supplierid")))
                    ocmd.Parameters.Add(New OleDbParameter("@catalogid", mainDatatable.Rows(i)("catalogid")))
                    ocmd.Parameters.Add(New OleDbParameter("@ccode", mainDatatable.Rows(i)("ccode")))
                    ocmd.Parameters.Add(New OleDbParameter("@cname", mainDatatable.Rows(i)("cname")))
                    ocmd.Parameters.Add(New OleDbParameter("@pother2", mainDatatable.Rows(i)("pother2")))
                    ocmd.Parameters.Add(New OleDbParameter("@pother3", mainDatatable.Rows(i)("pother3")))
                    ocmd.Parameters.Add(New OleDbParameter("@cprice", mainDatatable.Rows(i)("cprice")))
                    ocmd.Parameters.Add(New OleDbParameter("@rank", mainDatatable.Rows(i)("rank")))
                    ocmd.Parameters.Add(New OleDbParameter("@rankchange", mainDatatable.Rows(i)("rankchange")))
                    ocmd.Parameters.Add(New OleDbParameter("@PD", mainDatatable.Rows(i)("PD")))
                    ocmd.Parameters.Add(New OleDbParameter("@PDP", mainDatatable.Rows(i)("PDP")))
                    ocmd.Parameters.Add(New OleDbParameter("@TPD", mainDatatable.Rows(i)("TPD")))
                    ocmd.Parameters.Add(New OleDbParameter("@TPDP", mainDatatable.Rows(i)("TPDP")))
                    ocmd.Parameters.Add(New OleDbParameter("@sprice", mainDatatable.Rows(i)("sprice")))
                    ocmd.Parameters.Add(New OleDbParameter("@msprice", mainDatatable.Rows(i)("msprice")))
                    ocmd.Parameters.Add(New OleDbParameter("@minprice", mainDatatable.Rows(i)("minprice")))
                    ocmd.Parameters.Add(New OleDbParameter("@sopriceDP", mainDatatable.Rows(i)("sopriceDP")))
                    ocmd.Parameters.Add(New OleDbParameter("@pother4", mainDatatable.Rows(i)("pother4")))
                    ocmd.Parameters.Add(New OleDbParameter("@usedsuppliermargin", mainDatatable.Rows(i)("usedsuppliermargin")))
                    ocmd.Parameters.Add(New OleDbParameter("@cimageurl", mainDatatable.Rows(i)("cimageurl")))
                    ocmd.Parameters.Add(New OleDbParameter("@ccategory", mainDatatable.Rows(i)("ccategory")))
                    ocmd.Parameters.Add(New OleDbParameter("@pricetaggroup", mainDatatable.Rows(i)("pricetaggroup")))
                    ocmd.Parameters.Add(New OleDbParameter("@spricemargingroup", mainDatatable.Rows(i)("spricemargingroup")))

                    ocmd.ExecuteNonQuery()
                    ocmd.Parameters.Clear()

                Next
4

2 回答 2

1

我不知道如果你像问题中那样离开它是如何工作的,看看这一行

  cmd.Parameters.Add(New OleDbParameter("@sopriceDP", mainDatatable.Rows(i)("sopriceDP")))

并与命令比较

..@msprice, @minprice, @sopriceDB, @pother4,..

@sopriceDP 一方面,@sopriceDB另一方面

于 2012-09-14T03:15:47.520 回答
1

它应该是ocmd你必须添加参数而不是cmd.

ocmd.Parameters.Add(New OleDbParameter("@s....

还有一件事,你必须清除之后的参数ExecuteNonQuery

For i = 0 To mainDatatable.Rows.Count - 1
    ocmd.Parameters.Add(New OleDbParameter("@s....
    ....

    ocmd.ExecuteNonQuery()
    ocmd.Parameters.Clear()
Next
于 2012-09-14T02:50:48.457 回答