,
Share this with your friends 
 

CREATING CSV FILES IN VB.NET

0 ratings Views 1314 
Author: Daniel_7464bf92 (Daniel Odongo)  View Profile |  View other solutions by this author

Question / Problem


Create CSV file from records in a database. However, this is just a syntax to guide you. Where there is SQL, insert SQL select command

Solution

Try
con.Open()
cmd = New SqlCommand(sql, con)
dr = cmd.ExecuteReader

Dim filename As String = "d:\test.csv"

If Not File.Exists(filename) Then
File.Create(filename)
End If

Dim sw As New StreamWriter(filename)

If dr.HasRows Then
dr.Read()
sw.WriteLine("CSV HEADER")
sw.WriteLine("")

' CSV row items
While dr.Read = True
sw.WriteLine(dr(0) & "," & dr(1))
End While
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try

Applies to

VB 2005

Rank It

Login to rank it

Report


Advertisement