Results 1 to 5 of 5

Thread: Best way to put a contents of a dataset into a tab delimited file?

  1. #1

    Thread Starter
    Fanatic Member The_Grudge's Avatar
    Join Date
    Jan 2005
    Location
    Canada
    Posts
    836

    Best way to put a contents of a dataset into a tab delimited file?

    I have a program that queries multiple data sources, firing the results of each query into a single dataset.

    That part is working okay - I'm just now sure how best to proceed.

    I need to export this dataset to a fixed length, tab delimited file. Should I loop through the data set and write lines to a text file (in which case I could use a sample), or is there a better way ?

    Thanks.

  2. #2
    Hyperactive Member
    Join Date
    Nov 2008
    Location
    PA
    Posts
    365

    Re: Best way to put a contents of a dataset into a tab delimited file?

    Converting to VB shouldn't be hard... Reason being it's taken straight from one of my current applications I'm looking at atm:
    Code:
    using System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(PathToFile + "\\" FileName + ".txt"))
    {
        for(int i = 0; i = dataSet.Tables[0].Rows.Count; i ++)
        {
            streamWriter.WriteLine(dataSet.Tables[0].Rows[i, columnIndex]); //this writes each [row, column] value to a new line of the above txt file
            streamWriter.Flush(); //continually dump the stream to the file
        }
    }
    Last edited by detlion1643; May 31st, 2013 at 01:54 PM. Reason: forgot end code tag

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Best way to put a contents of a dataset into a tab delimited file?

    Fixed-length and tab delimited are somewhat mutually exclusive...

    fields are going to line up differently based on the contents of the field in a tab delimited file... fixed length on the other hand... means that all fields start at a given point (usually) on a row


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,398

    Re: Best way to put a contents of a dataset into a tab delimited file?

    I have a function in my signature that converts delimited textfiles to datatables and vice-versa. It includes things like: does it have a header? what do you want the delimiter to be? etc. Just check it out, it's here.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  5. #5
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Best way to put a contents of a dataset into a tab delimited file?

    Quote Originally Posted by techgnome View Post
    Fixed-length and tab delimited are somewhat mutually exclusive...
    Definitely.

    Tab delimited is easy so I thought I'd mess around with fixed-length and getting it to generate a format string from an array of field lengths. Negative numbers a left aligned and positive are right.

    vb.net Code:
    1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    2.  
    3.         Dim dt = GetDataTable()
    4.  
    5.         Dim fieldLengths = {-3, 10, 10, 5}
    6.  
    7.         WriteFixedLengthFile("C:\Temp\FixedTest.txt", dt, fieldLengths)
    8.  
    9.     End Sub
    10.  
    11.     Private Function GetDataTable() As DataTable
    12.         Dim dt As New DataTable
    13.  
    14.         With dt.Columns
    15.             .Add("ID", GetType(Integer)).AutoIncrement = True
    16.             .Add("Description", GetType(String))
    17.             .Add("Foo", GetType(String))
    18.             .Add("Bar", GetType(String))
    19.         End With
    20.  
    21.         With dt.Rows
    22.             .Add(Nothing, "abcdefg", "abcdefg", "abc")
    23.             .Add(Nothing, "hijklmn", "hijklmn", "hij")
    24.             .Add(Nothing, "opqrstu", "opqrstu", "opq")
    25.             .Add(Nothing, "vwxyz12", "vwxyz12", "vwx")
    26.         End With
    27.  
    28.         Return dt
    29.     End Function
    30.  
    31.     Private Sub WriteFixedLengthFile(fileName As String, dt As DataTable, fieldLengths As Integer())
    32.  
    33.         Dim formatString = GetFormatString(fieldLengths)
    34.  
    35.         Using sw As New IO.StreamWriter(fileName)
    36.             For Each row In dt.Rows.Cast(Of DataRow).ToArray()
    37.                 sw.WriteLine(String.Format(formatString, row.ItemArray))
    38.             Next
    39.         End Using
    40.  
    41.     End Sub
    42.  
    43.     Private Function GetFormatString(fieldLengths As Integer()) As String
    44.         Dim counter = 0
    45.         Dim formatString = ""
    46.  
    47.         For Each length In fieldLengths
    48.             formatString &= String.Format("{{{0},{1}}}", counter, length)
    49.             counter += 1
    50.         Next
    51.  
    52.         Return formatString
    53.     End Function
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width