在 VB.net WinForms 中获取数据库中的多行并将其传输到多行文本框中
Getting Multi Rows in Database and transferring it in a multiline textbox in VB.net WinForms
在我的代码中,我有一个数据库,其中包含 table 我的申请人。正如您将在下面的代码中看到的,我想从我的命令文本中获取行数并将其传输到字符串 "abc"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
myr.Close()
mycom.Connection = cn
mycom.CommandText = "SELECT Count(Cellphone) FROM tbl_applicant where Gender='Female';"
myr = mycom.ExecuteReader
Dim abc As String
If myr.Read Then
abc = myr(0)
End If
myr.Close()
在下面的代码中,我使用了 abc 作为我必须获取的数据的数量。然后我使用新查询获取我想要的值并将它们传输到字符串数组,如您所见,我将通用变量 Numb 重新调整为 abc 以使其具有数组边界。
mycom.CommandText = "SELECT Cellphone FROM tbl_applicant where Gender='Female';"
myr = mycom.ExecuteReader
ReDim Numb(abc)
If myr.Read Then
For i As Integer = 1 To abc.ToString - 1
LOT = myr(0).ToString
LOT = LOT + (myr(i).ToString + ",") <- this is where i get the error it says that index is our of range.
Numb = LOT.Split(",")
Next
End If
在下面的代码中,我希望将 Variable Numb() 的值传输到多行文本框
Dim sbText As New System.Text.StringBuilder(500)
For i As Integer = 0 To Numb.Length - 2
' This will convert the number to a string, add it to the stringbuilder
' and then append a newline to the text buffer
sbText.AppendLine(Numb(i))
Next i
' Now move the buffer into the control
TextBox1.Text = sbText.ToString()
End Sub
我必须在文本框中看到的最终值应该像
11111111111
11111111112
11111111113
11111111114
等等,请尝试将我指的数字理解为真实的 phone 数字。可能对问题或解决方案有任何帮助。谢谢
我认为您不需要先查询数据库来获取记录数,然后再返回数据库获取电话号码,您可以这样做:
mycom.CommandText = "SELECT Cellphone FROM tbl_applicant where Gender='Female';"
myr = mycom.ExecuteReader
While myr.Read()
TextBox1.Text = TextBox1.Text & myr(0) & Environment.NewLine
End While
不需要array
或List
虽然这只是一个粗略的指南并试图了解您的问题,但请尝试代码并查看它是否适合您。
在我的代码中,我有一个数据库,其中包含 table 我的申请人。正如您将在下面的代码中看到的,我想从我的命令文本中获取行数并将其传输到字符串 "abc"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
myr.Close()
mycom.Connection = cn
mycom.CommandText = "SELECT Count(Cellphone) FROM tbl_applicant where Gender='Female';"
myr = mycom.ExecuteReader
Dim abc As String
If myr.Read Then
abc = myr(0)
End If
myr.Close()
在下面的代码中,我使用了 abc 作为我必须获取的数据的数量。然后我使用新查询获取我想要的值并将它们传输到字符串数组,如您所见,我将通用变量 Numb 重新调整为 abc 以使其具有数组边界。
mycom.CommandText = "SELECT Cellphone FROM tbl_applicant where Gender='Female';"
myr = mycom.ExecuteReader
ReDim Numb(abc)
If myr.Read Then
For i As Integer = 1 To abc.ToString - 1
LOT = myr(0).ToString
LOT = LOT + (myr(i).ToString + ",") <- this is where i get the error it says that index is our of range.
Numb = LOT.Split(",")
Next
End If
在下面的代码中,我希望将 Variable Numb() 的值传输到多行文本框
Dim sbText As New System.Text.StringBuilder(500)
For i As Integer = 0 To Numb.Length - 2
' This will convert the number to a string, add it to the stringbuilder
' and then append a newline to the text buffer
sbText.AppendLine(Numb(i))
Next i
' Now move the buffer into the control
TextBox1.Text = sbText.ToString()
End Sub
我必须在文本框中看到的最终值应该像
11111111111
11111111112
11111111113
11111111114
等等,请尝试将我指的数字理解为真实的 phone 数字。可能对问题或解决方案有任何帮助。谢谢
我认为您不需要先查询数据库来获取记录数,然后再返回数据库获取电话号码,您可以这样做:
mycom.CommandText = "SELECT Cellphone FROM tbl_applicant where Gender='Female';"
myr = mycom.ExecuteReader
While myr.Read()
TextBox1.Text = TextBox1.Text & myr(0) & Environment.NewLine
End While
不需要array
或List
虽然这只是一个粗略的指南并试图了解您的问题,但请尝试代码并查看它是否适合您。