使用 SqlDataReader vb.net 为每个创建一个 [英] Make a for each using SqlDataReader vb.net
问题描述
我需要将表 1 中的值IdMaterial"(假设有超过 40 条记录)存储到一个数组中,将所有记录保存到表 2 中的代码中,我将向您展示只保存第一条记录而不是全部记录.
i need store the value "IdMaterial" from table 1 ( imagine that have more that 40 records) into a array save all that reacord into table 2 on the code i will show you only save me the first record and not all.
我会感谢你的帮助,我是编程中的菜鸟
i will apreceate your help i a noobie in proraming
代码:
将 i 变暗为整数我 = 0
Dim i As Integer i = 0
Try
Dim mater As String
Dim planta As String
Dim almacen As String
Dim lot As String
Dim cantidad As String
Dim cantadiat As String
Dim undad As String
Dim Cantidadc As String
Dim CantidadB As String
Dim Session1 As String
Dim fecha As String
'''''
Dim Con34 As New Data.SqlClient.SqlConnection
Con34.ConnectionString = C.GetAppConfiguracion("Inventario", "ConnInventario")
Dim editCustQuery As String = "select * from dbo.s_RptInventarioSAP"
Con34.Open()
Using CustCommand As New SqlCommand(editCustQuery, Con34)
Dim dr As SqlDataReader = CustCommand.ExecuteReader()
dr.Read()
mater = dr.GetString(0)
planta = dr.GetString(1)
almacen = dr.GetString(2)
lot = dr.GetString(3)
cantidad = dr.GetString(4)
cantadiat = dr.GetString(5)
undad = dr.GetString(6)
Cantidadc = dr.GetString(7)
CantidadB = dr.GetString(8)
Session1 = dr.GetString(9)
fecha = dr.GetDateTime(10)
end using
Dim Con As New Data.SqlClient.SqlConnection
Dim StrSQL As String
Con.ConnectionString = C.GetAppConfiguracion("Inventario", "ConnInventario")
StrSQL = ""
StrSQL = "EXEC P_AsigDupla '" & Txtfecha.Text & "','" & cboPlanta0.SelectedValue & "', '" & cboPlanta0.SelectedItem.Text & "','" & cboAlmacen.SelectedValue & "', '" & cboAlmacen.SelectedItem.Text & "', '" & mater & "', '" & lot & "'"
Con.Open()
Dim CmdAd As New Data.SqlClient.SqlCommand(StrSQL, Con)
CmdAd.ExecuteNonQuery()
Con.Close()
i = i + 1
'Next
Catch ex As Exception
lbError0.Text = ex.Message
End Try
End If
End Sub
推荐答案
该评论没有足够的字符来回答第二个问题,所以就在这里.
The comment did not have enough characters for the answer for the second issue so here it goes.
好的,我看到您正在使用数组 mater 和 lot 中的值,但您必须定义数组中的哪个值.
Ok, I see you are using values from the array mater, and lot, but you have to define which one of the values inside the array.
mater() 和 lot() 与数据库中表的行数一样长.因此,如果该表上有 20 行,则数组 lot 将有 20 个元素,从 lot(0) 到 lot(19),其他元素相同.
mater() and lot() are as long as the amount of rows of the of the table in the database. So if you have 20 rows on that table, the array lot will have 20 elements going from lot(0) to lot(19) and the same for the others.
如果您需要为每一行获取 resultado,那么您必须将 resultado() 定义为一个数组并使用相同的 FOR 来填充它.
If you need to get resultado for each row, then you have to define resultado() as an array and use the same FOR to fill it.
resultado(RowCounter)=GM.AsigDupla(Txtfecha.Text, cboPlanta0.SelectedValue, cboPlanta0.SelectedItem.Text, cboAlmacen.SelectedValue, cboAlmacen.SelectedItem.Text, mater(RowCounter), lot(RowCounter)
希望这能解决您的问题.
I hope this solve your issue.
这篇关于使用 SqlDataReader vb.net 为每个创建一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!