插入带有变量的SQL和变量在循环中是不是在改变(vb.net,postgresql)?

时间:2013-08-10 15:27:15

标签: vb.net postgresql

我正在开发一个应用程序,用于将数据从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作为新数据集         Dim strConnString As String         Dim strSQL As String

    '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

`

1 个答案:

答案 0 :(得分: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
        ...