插入带有变量的SQL,并且变量在循环中未更改(vb.net、postgresql)?
我正在开发一个将csv数据插入postgresql数据库的应用程序。我填充一个datagrid,在datagrid中循环,并在表中插入一条记录(是的,我知道代码非常冗长,但出于测试目的,我希望现在就这样做)。一切似乎都很完美;在运行代码和调试时,变量在循环过程中会发生变化,但是在插入到数据库中时,它只使用每个新的插入而不是新的变量值插入第一行的数据 想法?建议 下面是完整的代码:`Me.btnpouplatedata.Enabled=True Dim objConn作为新System.Data.Odbc.OdbcConnection Dim objCmd作为新的System.Data.Odbc.OdbcCommand Dim dtAdapter作为新System.Data.Odbc.OdbcDataAdapter Dim ds作为新数据集 作为字符串的Dim STRCONSTRING 作为字符串的Dim strSQL插入带有变量的SQL,并且变量在循环中未更改(vb.net、postgresql)?,vb.net,postgresql,Vb.net,Postgresql,我正在开发一个将csv数据插入postgresql数据库的应用程序。我填充一个datagrid,在datagrid中循环,并在表中插入一条记录(是的,我知道代码非常冗长,但出于测试目的,我希望现在就这样做)。一切似乎都很完美;在运行代码和调试时,变量在循环过程中会发生变化,但是在插入到数据库中时,它只使用每个新的插入而不是新的变量值插入第一行的数据 想法?建议 下面是完整的代码:`Me.btnpouplatedata.Enabled=True Dim objConn作为新System.Data.
'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
`您应该在循环外部添加参数,然后使用以下方法在循环内部设置参数值:
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
...
塔里克,我被你的建议搞糊涂了。。。我的前三个参数基于通过datagrid行的循环。。。我该如何设置这些“外部循环”?例如,this.Parameters.AddWithValue(“name”,strName)实际上是当前datagrid行的第一个单元格,strName在循环期间会更改,但sql语句不会将其插入表中。你能修改我的代码以反映你将如何编写这个代码吗?谢谢通过cmd.parameters.Append cmd.CreateParameter(“paramname”,adVarChar,adParamInput,20)在循环外创建参数,然后在循环内通过.parameters(“…”)设置这些参数的值。值=…确定,我尝试了,但得到一个错误:“Append”不是“System.Data.Odbc.OdbcParameterCollection”的成员。我正在使用Visual Studio 2005 Pro btw,如果这有什么不同的话。不过,你让我走上了正确的道路,谢谢!以下是有效的方法:.Parameters.Add(“name”,Odbc.OdbcType.NVarChar)在循环外部,而.Parameters(“name”).Value=strName在循环内部。谢谢你的帮助!我没有Windows机器来测试它。但关键是这个想法。顺便说一下,将命令的prepared属性设置为true以加快插入速度。