Click to See Complete Forum and Search --> : Retriving Table Columns Using ASP


Asim
03-29-2004, 12:58 PM
Hi guys

I have to retrive number of colums and their names of a particular table.

Let say i have a table A and i want to show the name of its field but i dont know how many fields table A have and what are their name.

Remember the name of table is dynamic means i will provide the name from my form field and then wanna show the structure of table.


Thnx in adv

Ribeyed
03-29-2004, 02:27 PM
hi,



set DBConn = Server.CreateObject("ADODB.Connection")
DBConn.Open "YourDatabase"

set tableSet = Server.CreateObject("ADODB.RecordSet")
tableSet.Open p_table_name, DBConn, adOpenForwardOnly, adLockOptimistic, adCmdTable

p_numberOfColums = tableSet.fields.count
for x = 0 to p_numberofColums

response.write tableSet.Fields(x).Name

while not tableSet.EOF
for each col in tableSet.Fields
response.write col.Value
next
tableSet.MoveNext
wend
tableSet.Close


Hope this helps

lcscne
03-29-2004, 02:32 PM
This should work for you:


Dim rs, objField

Set rs = Server.CreateObject("ADODB.RecordSet")
rs.Open yourTableName, yourConnection, _
0, 1, adCmdTable

For Each objField in rs.Fields
Response.Write objField.Name
Next

rs.Close
Set rs = Nothing


good luck