|
-
May 31st, 2013, 01:12 PM
#1
Thread Starter
Fanatic Member
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.
-
May 31st, 2013, 01:53 PM
#2
Hyperactive Member
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
-
May 31st, 2013, 02:19 PM
#3
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
-
May 31st, 2013, 02:32 PM
#4
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.
-
May 31st, 2013, 03:48 PM
#5
Re: Best way to put a contents of a dataset into a tab delimited file?
 Originally Posted by techgnome
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:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dt = GetDataTable()
Dim fieldLengths = {-3, 10, 10, 5}
WriteFixedLengthFile("C:\Temp\FixedTest.txt", dt, fieldLengths)
End Sub
Private Function GetDataTable() As DataTable
Dim dt As New DataTable
With dt.Columns
.Add("ID", GetType(Integer)).AutoIncrement = True
.Add("Description", GetType(String))
.Add("Foo", GetType(String))
.Add("Bar", GetType(String))
End With
With dt.Rows
.Add(Nothing, "abcdefg", "abcdefg", "abc")
.Add(Nothing, "hijklmn", "hijklmn", "hij")
.Add(Nothing, "opqrstu", "opqrstu", "opq")
.Add(Nothing, "vwxyz12", "vwxyz12", "vwx")
End With
Return dt
End Function
Private Sub WriteFixedLengthFile(fileName As String, dt As DataTable, fieldLengths As Integer())
Dim formatString = GetFormatString(fieldLengths)
Using sw As New IO.StreamWriter(fileName)
For Each row In dt.Rows.Cast(Of DataRow).ToArray()
sw.WriteLine(String.Format(formatString, row.ItemArray))
Next
End Using
End Sub
Private Function GetFormatString(fieldLengths As Integer()) As String
Dim counter = 0
Dim formatString = ""
For Each length In fieldLengths
formatString &= String.Format("{{{0},{1}}}", counter, length)
counter += 1
Next
Return formatString
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|