0

我正在开发一个应用程序,将 csv 中的数据插入到 postgresql 数据库中。我填充了一个数据网格,遍历数据网格并将一条记录插入到表中(是的,我知道代码非常冗长,但我现在就希望它这样用于测试目的)。一切似乎都运行良好;在运行代码和调试时,变量在循环期间会发生变化,但是,在插入数据库时​​,它仅在每次新插入时插入第一行中的数据,而不是新变量值。

想法?建议?

这是完整的代码:`Me.btnPoupulateData.Enabled = True Dim objConn As New System.Data.Odbc.OdbcConnection Dim objCmd As New System.Data.Odbc.OdbcCommand Dim dtAdapter As New System.Data.Odbc.OdbcDataAdapter Dim ds As New DataSet 将 strConnString 作为字符串 将 strSQL 作为字符串进行调暗

    'these are the required fields for the table product_template
    'catc null vals as exceptions
    Dim str_mes_type As String = "fixed"
    Dim i_uom_id As Integer = 1
    Dim i_uom_po_id As Integer = 1
    Dim strtype As String = "product"
    Dim str_procure_method As String = "make_to_stock"
    Dim str_cost_method As String = "standard"
    Dim i_categ_id As Integer = 1
    Dim str_supply_method As String = "buy"
    Dim str_sale_ok As String = True

    Dim str_import_date As String = Me.txtImportID.Text

    Dim dgv As DataGridView = DataGridView1
    Dim iImportCounter As Integer = 0

    System.Windows.Forms.Cursor.Current = Cursors.WaitCursor

    strConnString = "Dsn=PostgreSQL35W;database=OpenERP;server=localhost;port=5432;uid=openpg;pwd=openpgpwd"
    objConn.ConnectionString = strConnString
    objConn.Open()

    strSQL = "INSERT INTO product_template (name,description,standard_price,list_price,mes_type,uom_id,uom_po_id,type,procure_method,cost_method,categ_id,supply_method,sale_ok,import_date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"

    dtAdapter.SelectCommand = objCmd
    objCmd.Connection = objConn

    Try
        For i As Integer = 0 To dgv.RowCount - 1

            'workaround for the problem of not exiting the loop when at end of rows
            If dgv.RowCount = 1 Then
                Exit Try
            End If

            iImportCounter = iImportCounter + 1
            Me.lblRecordsImported.Text = "Records imported: " & iImportCounter


            Dim r As DataGridViewRow = dgv.Rows(i)

            '*************these are the changeable variables******
            With objCmd
                .Parameters.Add("name", Odbc.OdbcType.NVarChar)
                .Parameters.Add("description", Odbc.OdbcType.NVarChar)
                .Parameters.Add("standard_price", Odbc.OdbcType.NVarChar)
                .Parameters.Add("list_price", Odbc.OdbcType.NVarChar)
            End With

            'name goes to default code which is the internal reference number
            Dim strName As String
            strName = dgv.Rows(i).Cells(0).Value
            Dim str_description As String
            str_description = dgv.Rows(i).Cells(1).Value
            Dim i_standard_price As String
            i_standard_price = dgv.Rows(i).Cells(2).Value
            Dim i_list_price As String
            i_list_price = dgv.Rows(i).Cells(3).Value


            With objCmd
                'number of parameters must equal number of ? marks in sql statement
                '14 params now
                '.Parameters.AddWithValue used only for data that's constant
                .Parameters("name").Value = strName
                .Parameters("description").Value = str_description
                .Parameters("standard_price").Value = i_standard_price
                .Parameters("list_price").Value = i_list_price
                .Parameters.AddWithValue("mes_type", str_mes_type)
                .Parameters.AddWithValue("uom_id", i_uom_id)
                .Parameters.AddWithValue("uom_po_id", i_uom_po_id)
                .Parameters.AddWithValue("type", strtype)
                .Parameters.AddWithValue("procure_method", str_procure_method)
                .Parameters.AddWithValue("cost_method", str_cost_method)
                .Parameters.AddWithValue("categ_id", i_categ_id)
                .Parameters.AddWithValue("supply_method", str_supply_method)
                .Parameters.AddWithValue("sale_ok", str_sale_ok)
                '*******created new column in product_template called import_date*******
                'type set to char verying, to be used for later searching
                .Parameters.AddWithValue("import_date", str_import_date)

                .CommandText = strSQL
                .ExecuteNonQuery()

                'delete the gridview row after import
                dgv.Rows.RemoveAt(i)
                Application.DoEvents()

            End With
        Next

    Catch ex As Exception   ' 
        'this is my way to resume next since there are errors on specific data rows that
        'will be ignored
        ImportCSV()

    End Try

    objConn.Close()

    System.Windows.Forms.Cursor.Current = Cursors.Default

`

4

1 回答 1

0

您应该在循环外添加一次参数,然后使用以下方法在循环内设置参数值:

With objCmd
    .Parameters.Append objCmd.CreateParameter("name", adVarChar, adParamInput, 20)
    ...
End With


For i As Integer = 0 To dgv.RowCount - 1

    Dim r As DataGridViewRow = dgv.Rows(i)
    Dim strName As String = dgv.Rows(i).Cells(0).Value
    Dim i_standard_price As String = dgv.Rows(i).Cells(1).Value
    Dim i_list_price As String = dgv.Rows(i).Cells(2).Value


    With objCmd
        'number of parameters must equal number of ? marks in sql statement
        .Parameters("name").Value = strName
        ...
于 2013-08-10T15:48:37.990 回答