使用datareader将多行数据读入同一文本框。 [英] Reading multi-rows of data into the same textbox with datareader.
问题描述
我无法弄清楚我做错了什么。我试图将多行数据读入同一文本框。我正在使用Datareader。让我解释一下我想要做什么。
我正在申请中创建客户备注/评论部分。我想存储有关客户服务对话的注释/评论。我正在用日期&存储它们评论被添加的时间。它存储在Access数据库中。我想检索数据并将其显示在winform上。我选择了一个多行文本框。我可以将数据显示在文本框中,但它只显示具有多个记录的客户的最后一条记录。
我不确定是否可能,有没有更好的方式或我错过了什么。
我尝试过:
CustomerNotes表:
CusNumber日期/时间评论
1350 3/29/2019 8:52:42 AM客户想要退款。
1376 8/23/2018 10:52:42 PM他们对产品不满意。
1350 4/4/2019 2:43:11 PM我们发出退款。
Textbox结果我想要:
3/29/2019 8:52:42 AM
客户想要退款。
4/4/2019 2:43:11 PM
我们发出退款。
我的代码:
尝试
Dim command = 新 OleDb.OleDbCommand
致电 connection()
Dim drCustomerNotes As OleDbDataReader = Nothing
使用 cm
.Connection = con
.CommandText = SELECT * FROM tblCusNotes WHERE fldCusNote sAcctNo = @CusAcctNo
尝试
.Parameters.Clear()
.Parameters.Add( @ CusAcctNo,OleDbType。 Integer )。值= tbCusAcctNo.Text
Catch ex As 异常
MsgBox(ex.Message, MsgBoxStyle.Critical)
结束 尝试
drCustomerNotes = .ExecuteReader
结束 使用
drCustomerNotes .Read()
CusNotesTemp = drCustomerNotes( fldCusNotesDateEntered)。ToString
CusNotesTemp = CusNotesTemp + vbCrLf + drCustomerNotes( fldCusNotesText)。ToString + vbCrLf
tbCusNotes。 Text = CusNotesTemp
CusNotesfound = True
End while
' 如果CusNotesfound = True则MsgBox(Cus Notes Info Found。,MsgBoxStyle.Information)
如果 CusNotesfound = False 然后 MsgBox( Cus Notes Info Not Found。,MsgBoxStyle.Information)
drCustomerNotes.Close()
我用上面的代码得到的文本框结果:
4 / 4/2019 2:43:11 PM
我们发出了退款。
也许下面的内容?将文本框移到循环外部,将循环内的字符串连接起来,然后将其绑定到文本框
CusNotesTemp =
drCustomerNotes.Read()
' CusNotesTemp = drCustomerNotes(fldCusNotesDateEntered)。ToString
CusNotesTemp = CusNotesTemp + drCustomerNotes( fldCusNotesDateEntered)。ToString + vbCrLf + drCustomerNotes ( fldCusNotesText)。ToString + vbCrLf
' tbCusNotes.Text = CusNotesTemp
CusNotesfound = True
结束 while
tbCusNotes.Text = CusNotesTemp
您也可以尝试连接StringBuilder类的字符串。
在.NET中使用StringBuilder类| Microsoft Docs [ ^
I can't figure out what I am doing wrong. I am trying to read multiple rows of data into the same textbox. I am using a Datareader. let me explain what I am trying to do.
I am creating a customer notes/comments section in my application. I want to store notes/comments about customer service conversation. I am storing them with the date & time the comment was added. This is stored in a Access database. I want to retrieve the data and display it on a winform. I have choosen a multi-line textbox. I can get the data to display in the textbox but it only displays the last record of a customer that has multiple records.
I'm not sure if it is possible, is there a better way or what am I missing.
What I have tried:
CustomerNotes table:
CusNumber Date/Time Comment
1350 3/29/2019 8:52:42 AM Customer is wanting a refund.
1376 8/23/2018 10:52:42 PM They were not happy with the product.
1350 4/4/2019 2:43:11 PM We issued a refund.
Textbox result that I want:
3/29/2019 8:52:42 AM
Customer is wanting a refund.
4/4/2019 2:43:11 PM
We issued a refund.
My code:
Try
Dim command = New OleDb.OleDbCommand
Call connection()
Dim drCustomerNotes As OleDbDataReader = Nothing
With cm
.Connection = con
.CommandText = "SELECT * FROM tblCusNotes WHERE fldCusNotesAcctNo = @CusAcctNo"
Try
.Parameters.Clear()
.Parameters.Add("@CusAcctNo", OleDbType.Integer).Value = tbCusAcctNo.Text
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
drCustomerNotes = .ExecuteReader
End With
While drCustomerNotes.Read()
CusNotesTemp = drCustomerNotes("fldCusNotesDateEntered").ToString
CusNotesTemp = CusNotesTemp + vbCrLf + drCustomerNotes("fldCusNotesText").ToString + vbCrLf
tbCusNotes.Text = CusNotesTemp
CusNotesfound = True
End While
'If CusNotesfound = True Then MsgBox("Cus Notes Info Found.", MsgBoxStyle.Information)
If CusNotesfound = False Then MsgBox("Cus Notes Info Not Found.", MsgBoxStyle.Information)
drCustomerNotes.Close()
Textbox result that I get with the above code:
4/4/2019 2:43:11 PM
We issued a refund.
Maybe something like below? Move the textbox outside the loop, concatenating the string inside the loop, then bind it to thetextbox
CusNotesTemp = "" While drCustomerNotes.Read() ' CusNotesTemp = drCustomerNotes("fldCusNotesDateEntered").ToString CusNotesTemp = CusNotesTemp + drCustomerNotes("fldCusNotesDateEntered").ToString + vbCrLf + drCustomerNotes("fldCusNotesText").ToString + vbCrLf ' tbCusNotes.Text = CusNotesTemp CusNotesfound = True End While tbCusNotes.Text = CusNotesTemp
You can also try concatenate the string with StringBuilder class.
Using the StringBuilder Class in .NET | Microsoft Docs[^]
这篇关于使用datareader将多行数据读入同一文本框。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!