Select returns field values.

Hi

I am Querying a MSSQL dataset in VB.NET to export table values to .csv files on local computer.

All code is working nicely however Data is arriving without field headers. Is there a different syntax to use to get the entire dataset.

Thanks
FLOG51
Private Sub txtExport_CSV_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtExport_CSV.Click
        connSQL.Open()
        Dim fromTBL_List As Data.DataTable
        Dim fromROW_List As Data.DataRow
        Dim tblNAME As String
        Dim table_schema As String
        Dim field_Value As String
        Dim sw As StreamWriter
        Dim line As String
 
        fromTBL_List = connSQL.GetSchema("Tables")
 
        For Each fromROW_List In fromTBL_List.Rows
            tblNAME = fromROW_List.Item("Table_Name").ToString
            sw = File.CreateText("d:\My Location\" & tblNAME & ".csv")
            MsgBox("Doing table " & tblNAME)
            table_schema = txtTable_Schema.Text
            'use the table_schema + table name to build the sql string
            If table_schema <> "" Then
                fromSQL = "SELECT * FROM " & table_schema & "." & tblNAME & ";"
            Else
                ' if table_schema already known add in '<your_table_schema>' below
                fromSQL = "SELECT * FROM <your_table_schema>." & tblNAME & ";"
            End If
            Dim FROMdp1 As SqlClient.SqlDataAdapter
            Dim FROMds1 As New DataSet()
            FROMdp1 = New SqlClient.SqlDataAdapter(fromSQL, connSQL)
            FROMdp1.Fill(FROMds1, "flex." & tblNAME)
            If FROMds1.Tables.Item(0).Rows.Count > 0 Then
                'start on each row
                For a = 0 To FROMds1.Tables(0).Rows.Count - 1
                    line = ""
                    For b = 0 To FROMds1.Tables(0).Columns.Count - 1
 
                        field_Value = FROMds1.Tables.Item(0).Rows(a).Item(b).ToString
                        line = line & field_Value & ","
 
                    Next
                    sw.WriteLine(line)
                Next
            End If
            MsgBox("Completed table" & tblNAME)
        Next
        connSQL.Close()
    End Sub

Open in new window

FLOG51Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

FLOG51Author Commented:
Worked it out , in case anyone needs this here it is:            

For Each fromcol In FROMds1.Tables.Item(0).Columns
                headerField = headerField & fromcol.Caption & "|"
            Next

            sw.WriteLine(headerField)

ADD the above code after:

            Dim FROMdp1 As SqlClient.SqlDataAdapter
            Dim FROMds1 As New DataSet()
            FROMdp1 = New SqlClient.SqlDataAdapter(fromSQL, connSQL)
            FROMdp1.Fill(FROMds1, "flex." & tblNAME)

FLOG51
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.