简体   繁体   English

使用vb.net从一列中检索多个数据

[英]retrieving multiple data from one column using vb.net

I m new to asp.net is there any way that i could display different data in one row where the data is pulled from same column of another table? 我是asp.net的新手,有什么办法可以在一行中显示不同数据的地方,该数据是从另一张表的同一列中提取的? where the column is name hello and contains 3 types of data let`s say my code is like this: 列是名称hello,其中包含3种类型的数据,假设我的代码是这样的:

    cmd1.CommandText = " SELECT hello FROM link where mode=@model and procedure =@procedure "
    cmd1.CommandType = System.Data.CommandType.Text
    Dim da1 As New SqlDataAdapter()
    da1.SelectCommand = cmd1
    Dim dt1 As New DataTable()
    da1.Fill(dt1)
    Dim myDataReader1 As SqlDataReader
    myDataReader1 = cmd1.ExecuteReader()
    Dim hello As String
    If myDataReader1.HasRows Then

        Do While myDataReader1.Read()
            hello= myDataReader1("hello").ToString()

        Loop
        lblhello.Text = hello
    Else
        lblhello.Text = ""
    End If


    myDataReader1.Close()  

my data output should be like: 我的数据输出应该像这样:

hihi,yoyo,heyhey 嗨,溜溜球,嘿嘿

but i ended up getting 但我最终得到了

heyhey 嘿嘿

only please help me on this issue 仅请在这个问题上帮助我

You are overwriting the hello string on every iteration in the Do while loop. 您将在Do while循环的每次迭代中覆盖hello字符串。 Therefore you are displaying only the last value as it is that is contained in that variable. 因此,您仅显示该变量中包含的最后一个值。

You need to concatenate the values on each iteration. 您需要在每次迭代中串联值。 You also need to add a , between each value to get the desired output. 您还需要在每个值之间添加,以获取所需的输出。

Change: 更改:

hello = myDataReader1("hello").ToString()

to

hello += myDataReader1("hello").ToString() & ","

or ... 要么 ...

hello = hello & myDataReader1("hello").ToString() & ","

EDIT: As the above solution will add an extra , at the end of the last added value to hello , you will need to remove it before showing the final string. 编辑:由于上述解决方案将在hello的最后一个添加值的末尾添加一个extra ,因此您需要在显示最终字符串之前将其删除。

Replace 更换

lblhello.Text = hello

by 通过

lblhello.Text = hello.Text.Substring(0, hello.Text.Length - 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM