A couple of weeks ago I posted an example on how to make a DSNless connection to a database in one of the threads. I didn't test the code, and once I did I found some things were imperfect.
So, did you ever dream about creating a DSNless connection to a database (like you can do with ASP)? Well, neither did I, but it's fun to see what you can do with a little CFSCRIPT and ColdFusion's COM support.
This should do it for all you brave programmers
<cfobject type="COM"
name="MyConn"
class="ADODB.Connection"
action="CREATE">
<CFSCRIPT>
DSNtemp = "DRIVER={Microsoft Access Driver (*.mdb)};";
DSNtemp = DSNtemp & "DBQ=d:\data\mydb.mdb";
MyConn.Open("#DSNtemp#", "", "", -1);
SQL = "SELECT * FROM MyTable";
MyRecordset = MyConn.Execute(SQL, 0, 8);
MyFields = MyRecordset.Fields;
/* Make a variable for recordcount */
RecordCount = 0;
while(NOT MyRecordset.EOF){
RecordCount = RecordCount + 1;
MyRecordset.MoveNext();
}
Columns = "";
</CFSCRIPT>
<!--- Get the Column Name from the MyFields collections --->
<CFLOOP COLLECTION="#MyFields#" ITEM="this">
<CFSET Columns = ListAppend(Columns, this.Name)>
</CFLOOP>
<!--- Populate a newly made query with columns --->
<CFSET MyRecords = QueryNew(Columns)>
<!--- Add 'RecordCount' rows to hold the data --->
<CFSET QueryAddRow(MyRecords, RecordCount)>
<!--- Go to the first row of the recordset --->
<CFSET MyRecordset.MoveFirst()>
<!--- And populate the recordset... --->
<CFLOOP FROM="1" TO="#RecordCount#" INDEX="i">
<CFLOOP COLLECTION="#MyFields#" ITEM="this">
<CFSET QuerySetCell(MyRecords, Trim(this.name), this.value, i)>
</CFLOOP>
<CFSET MyRecordset.MoveNext()>
</CFLOOP>
<!--- Ouput the data like this: --->
<CFOUTPUT QUERY="MyRecords">
<TR>
<CFLOOP LIST="#MyRecords.ColumnList#" INDEX="this">
<TD>#Evaluate(this)#</TD>
</CFLOOP>
</TR>
</CFOUTPUT>
</TABLE>
<!--- Close the ODBC connection (like you do in ASP)--->
<CFSET MyConn.Close()>
Have fun with it, but remember querying databases like this is much slower than the traditional CF way.
<webguru>iqof188</webguru>
So, did you ever dream about creating a DSNless connection to a database (like you can do with ASP)? Well, neither did I, but it's fun to see what you can do with a little CFSCRIPT and ColdFusion's COM support.
This should do it for all you brave programmers
<cfobject type="COM"
name="MyConn"
class="ADODB.Connection"
action="CREATE">
<CFSCRIPT>
DSNtemp = "DRIVER={Microsoft Access Driver (*.mdb)};";
DSNtemp = DSNtemp & "DBQ=d:\data\mydb.mdb";
MyConn.Open("#DSNtemp#", "", "", -1);
SQL = "SELECT * FROM MyTable";
MyRecordset = MyConn.Execute(SQL, 0, 8);
MyFields = MyRecordset.Fields;
/* Make a variable for recordcount */
RecordCount = 0;
while(NOT MyRecordset.EOF){
RecordCount = RecordCount + 1;
MyRecordset.MoveNext();
}
Columns = "";
</CFSCRIPT>
<!--- Get the Column Name from the MyFields collections --->
<CFLOOP COLLECTION="#MyFields#" ITEM="this">
<CFSET Columns = ListAppend(Columns, this.Name)>
</CFLOOP>
<!--- Populate a newly made query with columns --->
<CFSET MyRecords = QueryNew(Columns)>
<!--- Add 'RecordCount' rows to hold the data --->
<CFSET QueryAddRow(MyRecords, RecordCount)>
<!--- Go to the first row of the recordset --->
<CFSET MyRecordset.MoveFirst()>
<!--- And populate the recordset... --->
<CFLOOP FROM="1" TO="#RecordCount#" INDEX="i">
<CFLOOP COLLECTION="#MyFields#" ITEM="this">
<CFSET QuerySetCell(MyRecords, Trim(this.name), this.value, i)>
</CFLOOP>
<CFSET MyRecordset.MoveNext()>
</CFLOOP>
<!--- Ouput the data like this: --->
<CFOUTPUT QUERY="MyRecords">
<TR>
<CFLOOP LIST="#MyRecords.ColumnList#" INDEX="this">
<TD>#Evaluate(this)#</TD>
</CFLOOP>
</TR>
</CFOUTPUT>
</TABLE>
<!--- Close the ODBC connection (like you do in ASP)--->
<CFSET MyConn.Close()>
Have fun with it, but remember querying databases like this is much slower than the traditional CF way.
<webguru>iqof188</webguru>