Question .mdb file to delimited text file

aar0n

Member
Joined
Jan 17, 2008
Messages
19
Location
40 Miles S. of Nashville, TN
Programming Experience
5-10
Hi all,

I've had no problem finding info on how to import a text file into Access and SQL Server using VB.NET, but for some reason I'm having trouble finding code to do the opposite...taking an .mdb file and writing it out to a delimited text file. I'm using a user-defined xml configuration file to determine the delimiter (I think I've got that part covered). It will need to be one table per file.

This is just one of many functions of this application and I've been doing okay so far with the rest.

I'm using odbc as my connection as it eventually won't be limited to just Access.

If someone could provide some basic code and point me in the right direction I'd greatly appreciate it! I should be able to take it from there.
 
VB.NET:
	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
	Handles Button2.Click

		CreateCSVFile(myDataTable, "C:\Temp\MyFile.txt", "|")

	End Sub

VB.NET:
	Public Sub CreateCSVFile(ByVal dt As DataTable, ByVal FilePath As String, ByVal FileDelimiter As String)

		Dim sw As StreamWriter = New StreamWriter(FilePath, False)
		Dim ColumnCount As Integer = dt.Columns.Count

		For i As Integer = 0 To ColumnCount - 1
			sw.Write(dt.Columns(i))
			If i < ColumnCount - 1 Then
				sw.Write(FileDelimiter)
			Else
				sw.Write(sw.NewLine)
			End If
		Next

		For Each Row As DataRow In dt.Rows
			For i As Integer = 0 To ColumnCount - 1
				If Not Row.IsNull(i) Then
					sw.Write(Row.Item(i).ToString())
				End If
				If i < ColumnCount - 1 Then
					sw.Write(FileDelimiter)
				Else
					sw.Write(sw.NewLine)
				End If
			Next
		Next

		sw.Close()

	End Sub
 

Latest posts

Back
Top