PDA

Click to See Complete Forum and Search --> : Reading column names


kanejone
Oct 13th, 2000, 10:10 AM
--------------Posted in General as well-------------------
Hi guys.
I need to know how to read database column names and place them in a CSV file. Here's what I have to create the csv file, but instead of manually putting the headings in like I have below I want to know if I can read them in automatically depending on the SQL statement.

Thanks a lot for your help
Johnny




Function CreateCSVFile()

strFile = GenFileName()
Set fs = Server.CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile(server.MapPath(".") & "\" & strFile & ".csv",True)
If Not oRS.EOF Then
strtext = chr(34) & "Year" & chr(34) & ","
strtext = strtext & chr(34) & "Region" & chr(34) & ","
strtext = strtext & chr(34) & "Sales" & chr(34) & ","
a.WriteLine(strtext)
Do Until oRS.EOF
For i = 0 To oRS.fields.Count-1
strtext = chr(34) & oRS.fields(i) & chr(34) & ","
a.Write(strtext)
Next
a.Writeline()
oRS.MoveNext
Loop
End If
a.Close
Set fs=Nothing
Response.Write("Click <A HRef=" & strFile & ".csv>Here</A> to to get CSV file")
End Function


This is in VBScript.

monte96
Oct 13th, 2000, 02:31 PM
You could loop through the recordset's Fields collection and read the name property:


strHeader = ""
For intLoop = 0 to rs.Fields.Count - 1
strHeader = strHeader & rs.Fields(intLoop).Name & ", "
Next

strHeader = Left$(strHeader, len(strHeader) - 2)