簡體   English   中英

使用vb.net從一列中檢索多個數據

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

我是asp.net的新手,有什么辦法可以在一行中顯示不同數據的地方,該數據是從另一張表的同一列中提取的? 列是名稱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()  

我的數據輸出應該像這樣:

嗨,溜溜球,嘿嘿

但我最終得到了

嘿嘿

僅請在這個問題上幫助我

您將在Do while循環的每次迭代中覆蓋hello字符串。 因此,您僅顯示該變量中包含的最后一個值。

您需要在每次迭代中串聯值。 您還需要在每個值之間添加,以獲取所需的輸出。

更改:

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

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

要么 ...

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

編輯:由於上述解決方案將在hello的最后一個添加值的末尾添加一個extra ,因此您需要在顯示最終字符串之前將其刪除。

更換

lblhello.Text = hello

通過

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM