Results 1 to 2 of 2

Thread: Reading column names

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Location
    Ireland
    Posts
    224

    Question

    --------------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



    Code:
    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.

  2. #2
    Frenzied Member monte96's Avatar
    Join Date
    Sep 2000
    Location
    Somewhere in AZ
    Posts
    1,379
    You could loop through the recordset's Fields collection and read the name property:

    Code:
    strHeader = ""
    For intLoop = 0 to rs.Fields.Count - 1
        strHeader = strHeader & rs.Fields(intLoop).Name & ", "
    Next
    
    strHeader = Left$(strHeader, len(strHeader) - 2)
    oOOo--oOOo
    __/\/\onte96
    oOOo--oOOo
    Senior Programmer/Analyst
    MCP
    monte96_@hotmail.com
    monte96@vbgarage.com


    Your results may vary.. some restrictions may apply.. pricing and participation may vary.. not available in all states.. professional driver closed course..quantities limited..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width