Rip's Domain

DotNet DataSet To CF Structure of Queries

Posted in ColdFusion by rip747 on October 30, 2006

UPDATE: I don’t want to get comments or hate mail saying that I stole this code from Joe and called it my own. I give Joe credit and I even emailed him about it.

I have no idea why Joe Rinehart never submitted this little gem to (probably because it takes 6 months for your submission to show up). Any who, this is function that he wrote a while back that will take a DotNet Dataset and convert it to a CF structure of queries. I corrected some bugs (it would bomb if there was no record returned) and updated it (it now automatically converts xs:dateTime columns to CF datetime) and submitted it to CFLib. Hopefully someday it will make it into a library there, but for the mean time you can download it below.

I just want to say that Joe Rinehart deserves all the credit for this one. Thanks Joe!!!


<!— convertDotNetDataset —>
<cffunction name=”convertDotNetDataset” access=”public” returnType=”struct” output=”false”
hint=”takes a .Net dataset and converts it to a CF structure of queries”>
<cfargument name=”dataset” required=”true”>
<cfset var Local = StructNew()>
<cfset Local.result = structNew() />
<cfset Local.aDataset = arguments.dataset.get_any() />
<cfset Local.xSchema = xmlParse(Local.aDataset[1]) />
<cfset Local.xData = xmlParse(Local.aDataset[2]) />
<!— keeps track of which columns are xs:dateTime (1999-02-16T00:00:00.0000000-05:00) columns, so we can convert them —>
<cfset Local.DTColumns = StructNew()>

<!— Create Queries —>
<cfset Local.xTables = Local.xSchema["xs:schema"]["xs:element"]["xs:complexType"]["xs:choice"] />
<cfloop from=”1″ to=”#arrayLen(Local.xTables.xmlChildren)#” index=”Local.i”>
<cfset Local.tableName = Local.xTables.xmlChildren[Local.i] />
<cfset Local.xColumns = Local.xTables.xmlChildren[Local.i].xmlChildren[1].xmlChildren[1].xmlChildren/>
<cfset Local.result[Local.tableName] = queryNew(“”) />
<cfloop from=”1″ to=”#arrayLen(Local.xColumns)#” index=”Local.j”>
<cfif Local.xColumns[Local.j].xmlAttributes.type EQ “xs:dateTime”>
<cfset StructInsert(Local.DTColumns, Local.xColumns[Local.j], “”)>
<cfset queryAddColumn(Local.result[Local.tableName], Local.xColumns[Local.j], arrayNew(1)) />

<!— see if there are any row of data, if not exit —>
<cfif NOT StructKeyExists(Local.xData["diffgr:diffgram"], “NewDataSet”)>
<cfreturn Local.result>

<!— Populate Queries —>
<cfset Local.xRows = Local.xData["diffgr:diffgram"]["NewDataSet"] />
<cfloop from=”1″ to=”#arrayLen(Local.xRows.xmlChildren)#” index=”Local.i”>
<cfset Local.thisRow = Local.xRows.xmlChildren[Local.i] />
<cfset Local.tableName = Local.thisRow.xmlName />
<cfset queryAddRow(Local.result[Local.tableName], 1) />
<cfloop from=”1″ to=”#arrayLen(Local.thisRow.xmlChildren)#” index=”Local.j”>
<cfset Local.RowData = Local.thisRow.xmlChildren[Local.j].xmlText>
<cfset Local.ColumnName = Local.thisRow.xmlChildren[Local.j].xmlName>
<!— if this column is a xs:dateTime column, convert —>
<cfif StructKeyExists(Local.DTColumns, Local.ColumnName)>
<cfset Local.RowData = ReReplace(Local.RowData, “(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2,2}).*”, “\2/\3/\1 \4:\5:\6″)>
<cfset querySetCell(Local.result[Local.tableName], Local.ColumnName, Local.RowData, Local.result[Local.tableName].recordCount) />

<cfreturn Local.result>

About these ads

One Response

Subscribe to comments with RSS.

  1. Paul Baylis said, on December 15, 2010 at 10:57 pm

    This is awesome! Thanks for posting.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

%d bloggers like this: