Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

listing tbl fields using jscript 2

Status
Not open for further replies.

estafford

Programmer
Sep 5, 2002
22
0
0
US
Greetings,
I am looking for a way to list the field or column names of a table. i have found several examples using VBscript, but I need to do this using JScript or Javascript.

Here is what I have. (converted from a working sample in vbscript)

<%
var rsjsConn = Server.CreateObject(&quot;ADODB.Connection&quot;);
rsjsConn.Open(MM_forums_STRING);

rsjs = rsjsConn.Execute(&quot;select * from FORUM_FORUM&quot;)

var columnTypes = new Array(205)
columnTypes[0] = &quot;Empty&quot;
columnTypes[2] = &quot;SmallInt&quot;
columnTypes[3] = &quot;Integer&quot;
columnTypes[4] = &quot;Real&quot;
columnTypes[5] = &quot;Double&quot;
columnTypes[6] = &quot;Currency&quot;
columnTypes[7] = &quot;Date&quot;
columnTypes[8] = &quot;BSTR&quot;
columnTypes[9] = &quot;IDispatch&quot;
columnTypes[10] = &quot;Error Code&quot;
columnTypes[11] = &quot;Boolean&quot;
columnTypes[12] = &quot;Variant&quot;
columnTypes[13] = &quot;IUnknown&quot;
columnTypes[14] = &quot;Decimal&quot;
columnTypes[16] = &quot;TinyInt&quot;
columnTypes[17] = &quot;Unsigned TinyInt (BYTE)&quot;
columnTypes[18] = &quot;Unsigned Small Int (WORD)&quot;
columnTypes[19] = &quot;Unsigned Int (DWORD)&quot;
columnTypes[20] = &quot;BigInt&quot;
columnTypes[21] = &quot;Unsigned Big Int&quot;
columnTypes[64] = &quot;FileTime&quot;
columnTypes[72] = &quot;Unique Identifier (GUID)&quot;
columnTypes[128] = &quot;Binary&quot;
columnTypes[129] = &quot;Char&quot;
columnTypes[130] = &quot;nChar&quot;
columnTypes[131] = &quot;Numeric&quot;
columnTypes[132] = &quot;User Defined (UDT)&quot;
columnTypes[133] = &quot;DBDate&quot;
columnTypes[134] = &quot;DBTime&quot;
columnTypes[135] = &quot;SmallDateTime&quot;
columnTypes[136] = &quot;Chapter&quot;
columnTypes[138] = &quot;Automation (PropVariant)&quot;
columnTypes[139] = &quot;VarNumeric&quot;
columnTypes[200] = &quot;VarChar&quot;
columnTypes[201] = &quot;Text&quot;
columnTypes[202] = &quot;nVarChar&quot;
columnTypes[203] = &quot;nText&quot;
columnTypes[204] = &quot;VarBinary&quot;
columnTypes[205] = &quot;Image&quot;
Response.Write(&quot;list should start here&quot; )


for (column in rsjs.Fields){
Response.Write(column .Fields.Name + &quot; [&quot; + columnTypes[column .Fields.Type])
ct = column .Fields.Type
if (ct ==129 || ct == 130 || ct == 200 || ct == 202){
// definedSize only works in SQL Server
Response.write(&quot; (&quot; + column .Fields.definedSize + &quot;)&quot; )
}
Response.Write(&quot;]<br>&quot; )
}
%>

Problem is that the list is not created.
I also commented out the for/in loop to run only the inner statement like this :

Response.Write(rsjs.Fields.Name + &quot; [&quot; + columnTypes[rsjs.Fields.Type])
ct = rsjs.Fields.Type
if (ct ==129 || ct == 130 || ct == 200 || ct == 202){
// definedSize only works in SQL Server
Response.write(&quot; (&quot; + rsjs.Fields.definedSize + &quot;)&quot; )
}
Response.Write(&quot;]<br>&quot; )

Both rsjs.Fields.Name and rsjs.Fields.Type return &quot;undefined&quot; in the output.

Have I missed something?
 
estafford,

On the undefined issue, see:

function getStringField()
This function will convert a string that is defined as &quot;null&quot; or &quot;undefined&quot; in JScript to a blank string. getStringField() becomes especially useful when dealing with data as it comes from a recordset. It should be included in all of your ASP programs that interact with a database. You will no longer have to worry about the dreaded undefined.
function getStringField(theRSField) {
theStr = String(theRSField);
if (theStr == &quot;null&quot; || theStr == &quot;undefined&quot;)
return &quot;&quot;;
else
return theStr;
}


You appear to be going after information that is related to the schema of the database, and not the actual contents.

If that is what your looking for, suggest you see:


Try Google with a search string of &quot;OpenSchema var&quot; without the quotes.

Hope this helps.
DougCranston
 
here is an sample of modified code
Code:
<%@Language=JScript%>
<%
var rsjsConn = Server.CreateObject(&quot;ADODB.Connection&quot;);
var rsjs = Server.CreateObject(&quot;ADODB.Recordset&quot;);

rsjsConn.Open(connectionstringhere);

sql=&quot;select * from test1&quot;;
rsjs.Open(sql,rsjsConn,3,3);

    var columnTypes = new Object();
    columnTypes[&quot;0&quot;] = &quot;Empty&quot; 
    columnTypes[&quot;2&quot;] = &quot;SmallInt&quot; 
    columnTypes[&quot;3&quot;] = &quot;Integer&quot; 
    columnTypes[&quot;4&quot;] = &quot;Real&quot; 
    columnTypes[&quot;5&quot;] = &quot;Double&quot; 
    columnTypes[&quot;6&quot;] = &quot;Currency&quot; 
    columnTypes[&quot;7&quot;] = &quot;Date&quot; 
    columnTypes[&quot;8&quot;] = &quot;BSTR&quot; 
    columnTypes[&quot;9&quot;] = &quot;IDispatch&quot; 
    columnTypes[&quot;10&quot;] = &quot;Error Code&quot; 
    columnTypes[&quot;11&quot;] = &quot;Boolean&quot; 
    columnTypes[&quot;12&quot;] = &quot;Variant&quot; 
    columnTypes[&quot;13&quot;] = &quot;IUnknown&quot; 
    columnTypes[&quot;14&quot;] = &quot;Decimal&quot; 
    columnTypes[&quot;16&quot;] = &quot;TinyInt&quot; 
    columnTypes[&quot;17&quot;] = &quot;Unsigned TinyInt (BYTE)&quot; 
    columnTypes[&quot;18&quot;] = &quot;Unsigned Small Int (WORD)&quot; 
    columnTypes[&quot;19&quot;] = &quot;Unsigned Int (DWORD)&quot; 
    columnTypes[&quot;20&quot;] = &quot;BigInt&quot; 
    columnTypes[&quot;21&quot;] = &quot;Unsigned Big Int&quot; 
    columnTypes[&quot;64&quot;] = &quot;FileTime&quot; 
    columnTypes[&quot;72&quot;] = &quot;Unique Identifier (GUID)&quot; 
    columnTypes[&quot;128&quot;] = &quot;Binary&quot; 
    columnTypes[&quot;129&quot;] = &quot;Char&quot; 
    columnTypes[&quot;130&quot;] = &quot;nChar&quot; 
    columnTypes[&quot;131&quot;] = &quot;Numeric&quot; 
    columnTypes[&quot;132&quot;] = &quot;User Defined (UDT)&quot; 
    columnTypes[&quot;133&quot;] = &quot;DBDate&quot; 
    columnTypes[&quot;134&quot;] = &quot;DBTime&quot; 
    columnTypes[&quot;135&quot;] = &quot;SmallDateTime&quot; 
    columnTypes[&quot;136&quot;] = &quot;Chapter&quot; 
    columnTypes[&quot;138&quot;] = &quot;Automation (PropVariant)&quot; 
    columnTypes[&quot;139&quot;] = &quot;VarNumeric&quot; 
    columnTypes[&quot;200&quot;] = &quot;VarChar&quot; 
    columnTypes[&quot;201&quot;] = &quot;Text&quot; 
    columnTypes[&quot;202&quot;] = &quot;nVarChar&quot; 
    columnTypes[&quot;203&quot;] = &quot;nText&quot; 
    columnTypes[&quot;204&quot;] = &quot;VarBinary&quot; 
    columnTypes[&quot;205&quot;] = &quot;Image&quot; 
    Response.Write(&quot;list should start here<br>&quot;)
	var column=new Enumerator(rsjs.Fields)

	for (;!column.atEnd();column.moveNext())
	{
		var field=column.item();
		var type=(field.Type==129 || field.Type == 130 || field.Type == 200 || field.Type == 202)
		Response.Write(field.Name + &quot; [&quot; + (type? columnTypes[field.Type]+&quot;(&quot;+field.definedSize+&quot;)&quot; :columnTypes[field.Type]) );
        Response.Write(&quot;]<br>&quot; );
	} 
%>

________
George, M
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top