Based on the code you provided, I think the main problem is unnecessary loop nesting.
For Each column As DataColumn In ds.Tables(0).Columns
'Add the Data rows.
txt += row(column.ColumnName).ToString() & vbTab & vbTab
Next
When you traverse to each row of the same dataTable ( Tables(0) ), you will execute this loop, and it will get the same result. Therefore it is redundant, and it will reduce your operating efficiency.
I think you can move this part out of the outer loop, define the result as a variable and just use it. Something like this:
'txt += vbCr & vbLf
Dim columnNales as String
For Each column As DataColumn In ds.Tables(0).Columns
'Add the Data rows.
columnNales += row(column.ColumnName).ToString() & vbTab & vbTab
Next
If ds.Tables(0).Rows.Count > 0 Then
For Each row As DataRow In ds.Tables(0).Rows
'Add new line.
txt += columnNales & vbCr & vbLf
Next
fp.WriteLine(txt)
fp.Close() ... End If
Best regards,
Xudong Peng
ASP.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
Member
17 Points
37 Posts
Dataset to text file
Feb 25, 2021 01:55 PM|Mohammed Tajudeen|LINK
How to extract text file from dataset in VB.net . I tried below code but takes too much time. Is there any other solution available ?
Instead of loop it should be instance file.
'txt += vbCr & vbLf
If ds.Tables(0).Rows.Count > 0 Then
For Each row As DataRow In ds.Tables(0).Rows
For Each column As DataColumn In ds.Tables(0).Columns
'Add the Data rows.
txt += row(column.ColumnName).ToString() & vbTab & vbTab
Next
'Add new line.
txt += vbCr & vbLf
Next
fp.WriteLine(txt)
fp.Close()
Contributor
2400 Points
751 Posts
Re: Dataset to text file
Feb 26, 2021 04:30 AM|XuDong Peng|LINK
Hi Mohammed Tajudeen,
Based on the code you provided, I think the main problem is unnecessary loop nesting.
When you traverse to each row of the same dataTable ( Tables(0) ), you will execute this loop, and it will get the same result. Therefore it is redundant, and it will reduce your operating efficiency.
I think you can move this part out of the outer loop, define the result as a variable and just use it. Something like this:
Best regards,
Xudong Peng