gpt4 book ai didi

mysql - vb net 将多个数据读取器结果存储到一个字符串变量中

转载 作者:太空宇宙 更新时间:2023-11-03 12:17:48 26 4
gpt4 key购买 nike

我有一个类似SELECT ITEM FROM DETAIL WHERE TID="1" 的查询。这将返回像

这样的结果

m4、c1、f2、d5、k2

我正在使用 DATAREADER 获取多值结果

这是代码

Dim res as string = "SELECT ITEM FROM DETAIL WHERE TID='1'"
CMD = New MySqlCommand(res, con)
result = CMD.ExecuteReader()
while result.HasRows
result.Read()
array(indeks) = result("ITEM")
end while

现在改为将结果一个一个地存储到每个数组的索引中,

数组(0)=m4

array(1)=c1,.....

我想将所有这些存储到单个字符串变量中,格式'm4'、'c1'、'f2'、'd5'、'k2'

格式是单引号和逗号 (,) 作为每个结果的分隔符,如上例(逗号仅在仍有结果时出现)

我怎么能在 vb.net 中做到这一点?我正在使用 mysql 作为数据库

更新代码

    Dim cnt As String = "select count(*) from detail where kode_faktur= '" & 1 & "' "
Dim max As Int32
CMD_sup = New MySqlCommand(cnt, conn.konek)
max = Convert.ToInt32(CMD_sup.ExecuteScalar())
CMD_sup.Connection.Close()

Dim result As MySqlDataReader

Dim resultString As String
Dim isFirstResult = True

Dim arayITEM() As String
Dim res As String = "select kode_brg from detail where kode_faktur= '" & 1 & "' "
CMD = New MySqlCommand(res, conn.konek)
result = CMD.ExecuteReader()


ReDim arayITEM(max)
If result.HasRows Then
For i As Integer = 0 To max - 1
result.Read()
arayITEM(i) = result("kode_brg")
Next
End If

resultString = "'" & String.Join("','", arayITEM) & "'"
'MsgBox("HASIL : " & resultString)

这是截图

enter image description here

我不需要最后一个数组元素末尾的分隔符 (,'')

最佳答案

Dim res as string = "SELECT ITEM FROM DETAIL WHERE TID='1'"
CMD = New MySqlCommand(res, con)


' Read data from database
Dim result As New ArrayList()
Dr = CMD.ExecuteReader()

' Add each entry to array list
While Dr.Read()
' Insert each column into a dictionary
Dim dict As New Dictionary(Of String, Object)
For count As Integer = 0 To (Dr.FieldCount - 1)
dict.Add(Dr.GetName(count), Dr(count))
Next

' Add the dictionary to the ArrayList
result.Add(dict & ", ")
End While
Dr.Close()

所以,现在您可以像这样使用 for 循环遍历结果:

For Each dat As Dictionary(Of String, Object) In result
Console.Write(dat("ColName"))
Next

如果它只是 DataReader,这与您的做法非常相似:

While Dr.Read()
Console.Write(Dr("ColName"))
End While

代码来自:Reference我已经根据您的需要对其进行了修改,但没有进行测试。希望能帮到你。

关于mysql - vb net 将多个数据读取器结果存储到一个字符串变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21347659/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com