locked
Dataset to text file RRS feed

  • Question

  • User-1068576637 posted

    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()

    Thursday, February 25, 2021 1:55 PM

All replies

  • User-939850651 posted

    Hi Mohammed Tajudeen,

    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

    Friday, February 26, 2021 4:30 AM