【问题标题】:displaying all rows of a specific column in a textbox在文本框中显示特定列的所有行
【发布时间】:2013-12-26 05:01:17
【问题描述】:

我正在使用带有 sql server 2008 作为后端的 Visual Studio 2010,我在显示时遇到问题 文本框中特定列的所有行。 这个我试过了,

cmd.CommandText = "select article_no from  main where name='" & TextBox1.Text & "'"

cmd.Connection = con

 con.Open()

 Dim dr As SqlDataReader

dr = cmd.ExecuteReader

 If dr.HasRows Then

   dr.Read()

 TextBox2.Text = dr.Item("article_no")

 End If

con.Close()

但是我只能显示我在 textbox1 中输入的特定名称的第一行,而不是我需要显示包含在 textbox1 中输入的相同名称的所有行,并且应该显示在 textbox2 中。

所以请任何人帮助我了解所需的逻辑。

提前致谢

【问题讨论】:

    标签: vb.net visual-studio-2010 vb.net-2010


    【解决方案1】:

    也许您无法显示所有行,因为您使用的是文本框。文本框上只会添加 1 条记录,也可以通过将文本框多行属性设置为 true 来实现。我建议改用列表框,然后这样做

    If dr.HasRows Then
       While dr.Read()
         ListBox1.Add(dr.Item("article_no"))
       End While
    End If
    

    【讨论】:

      【解决方案2】:

      听起来您需要将 TextBox 设置为多行,或者您可能需要手动插入换行符。

      在此处查看相同的答案:(Similar Answer)

      【讨论】:

      • article_no 是数据库的列名,我需要从 article_no 列中检索由数据库的另一个列名“name”组成的所有值,请您帮忙。
      【解决方案3】:

      为了能够显示所有行,您需要在数据表中循环。试试这个;

      If dr.HasRows Then
      
       For Each _dr As DataRow In dr.Rows
      
         _dr.Read()
      
       TextBox2.Text = String.Concat(TextBox2.Text, ",", _dr.Item("article_no"))
      
       Next
      End If
      

      在你的文本框2中使用逗号,作为分隔符

      【讨论】:

        【解决方案4】:

        为什么不让它带有垂直滚动条的多行:

         TextBox2.Multiline = True      
         TextBox2.ScrollBars = ScrollBars.Vertical
        

        那么当你遍历时,你会这样做:

         TextBox2.Text &= dr.Item("article_no") & Environment.NewLine
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-07-20
          • 1970-01-01
          • 2012-08-29
          • 2014-08-15
          • 1970-01-01
          • 2013-01-23
          • 2014-12-13
          • 2014-06-18
          相关资源
          最近更新 更多