Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Read Data and Save as .CSV File
 

Read Data and Save as .CSV File

Hi,

I need to read data from SQL Server and save it as .csv file? How can I do it using vb.net?

Thanks!
MigrationUser 1  Wednesday, June 23, 2004 3:24 PM
you could do it something like this
Read the data with a data reader one record at a time. then create a string that represents
each field in the record separated by a comma then just write it to your file

Private Sub CreateFile()
        Dim conn As SqlConnection = New SqlConnection(connString)
        conn.Open()
        Dim sql As String = "SELECT * FROM MyTable;"
        Dim cmd As SqlCommand = New SqlCommand(sql, conn)
        Dim dr As SqlDataReader = cmd.ExecuteReader()
        Dim s As Stream = File.Open("C:\MyCsv.csv", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read)
        Dim w As StreamWriter = New StreamWriter(s)
        While (dr.Read())
            Dim s1 As String = ""
            Dim s2 As String = ""
            Dim s3 As String = ""
            If Not (dr(0) Is DBNull.Value) Then
                s1 = dr(0)
            End If
            If Not (dr(1) Is DBNull.Value) Then
                s2 = dr(1)
            End If
            If Not (dr(0) Is DBNull.Value) Then
                s3 = dr(2)
            End If
            Dim ss As String() = New String(2) {s1, s2, s3}
            WriteLine(w, ss)
        End While
        w.Close()
        dr.Close()
        conn.Close()
End Sub

Private Sub WriteLine(ByVal w As StreamWriter, ByVal strings As String())
        Dim sOut As String = ""
        For Each s As String In strings
            sOut = sOut + s + ","
        Next
        'Remove the last comma
        Dim index As Integer = sOut.LastIndexOf(",")
        sOut = sOut.Remove(index, 1)
        w.WriteLine(sOut)
End Sub
MigrationUser 1  Friday, June 25, 2004 11:33 AM
Thanks!
MigrationUser 1  Monday, June 28, 2004 9:04 AM
Just a note, you could speed this up quite a bit by using a stringbuilder object instead of a string object 

Also you wouldn't need a seperate string for each column.

You would read the row and then loop though the columns of the row and add the values like

value1,value2,value3 and so on

stringbuilderobject.append("value1,")

then when you get to the end

stringbuilderobject.append(environment.newline)


Hope this helps speed that up for you.
MigrationUser 1  Friday, July 02, 2004 11:26 AM

You can use google to search for other answers

Custom Search

More Threads

• When Datagridview error I could not close My Form, Why?
• error: Unbound reference in aggregate expression
• How to use Piechart for windows form
• DataView.RowFilter not working!!!
• DataGridView Showing Rows with no Data
• disable datagrid popup menu?
• Binding DataGridView to a DataTable - skipping binding some of the columns in the DataTable
• How to remove the £ sign from the insert statment
• Problem Searching DataGridView
• Derived DataGridView unbound column creation