PC Review


Reply
Thread Tools Rate Thread

convert recordset to dataset

 
 
Thaddeus
Guest
Posts: n/a
 
      17th Dec 2003
//Author:
//Thaddeus Jacobs, MCP
//Kinematic Automation, Inc.
//private.php?do=newpm&u=
//
//Description:
//convert ADO .NET dataset to ADO 2.5 2.6 2.7 recordset and v/v
//DataSet to recordset conversion.
//Get RecordSet from Dataset
//ds2rs DataSet2RecordSet
//
//I searched for minutes on end,
//figuring someone had posted code to do this.
//Basically it loops through the dataset, shooting ADO XML
//to an ado stream, then loading a stream into the returned recordset.
//A COM reference to Microsoft Activex Data Objects 2.7 was used,
//as well as an additional reference to System.Web (for the HTMLEncode
function)
//Alternatively one could use the RecordSet object alone to build the
//RS from scratch.
//This code is incomplete as it doesn't take into account all the
possible
//DataTypes (just the ones our legacy DB uses).
//This is only meant as a guideline, and by using this code, you
//hereby realize that you get what you pay for.
//
//Have a wonderful HAPPY HOLIDAYS!!!!, and make sure to spend some
quality
//time with your friends/family in the real world.

using System;
using ADODB;
using System.Data;
using System.Text;
using System.Reflection;
using System.Web;

namespace Kinematic
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class DataConvert
{
public DataConvert(){}

public ADODB.Recordset GetRecordSet(DataSet ds)
{
ADODB.Stream xmlstream = new ADODB.Stream();
xmlstream.Open( Missing.Value, ConnectModeEnum.adModeUnknown,
StreamOpenOptionsEnum.adOpenStreamUnspecified,
"", "" );

//Begin Schema
xmlstream.WriteText("<xml
xmlns:s=\"uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882\" " +
"xmlns:dt=\"uuid:C2F41010-65B3-11d1-A29F-00AA00C14882\" " +
"xmlns:rs=\"urn:schemas-microsoft-com:rowset\" " +
"xmlns:z=\"#RowsetSchema\">" +
"<s:Schema id=\"RowsetSchema\">" +
"<s:ElementType name=\"row\" content=\"eltOnly\">"
, StreamWriteEnum.stWriteChar);


//Add Fields To Schema
string dt;

foreach(DataColumn col in ds.Tables[0].Columns)
{
dt = "";
switch(System.Type.GetTypeCode(col.DataType))
{
case TypeCode.String:
dt = "<s:datatype dt:type=\"string\" dt:maxLength=\"1024\"
rsrecision=\"0\" rs:fixedlength=\"true\" />";
break;
case TypeCode.Int16:
dt = "<s:datatype dt:type=\"int\" dt:maxLength=\"4\"
rsrecision=\"10\" rs:fixedlength=\"true\"/>";
break;
case TypeCode.Int32:
dt = "<s:datatype dt:type=\"int\" dt:maxLength=\"4\"
rsrecision=\"10\" rs:fixedlength=\"true\"/>";
break;
case TypeCode.Int64:
dt = "<s:datatype dt:type=\"int\" dt:maxLength=\"4\"
rsrecision=\"10\" rs:fixedlength=\"true\"/>";
break;
case TypeCode.Decimal:
dt = "<s:datatype dt:type=\"float\" rsrecision=\"15\"
rs:fixedlength=\"true\"/>";
break;
case TypeCode.Double:
dt = "<s:datatype dt:type=\"float\" rsrecision=\"15\"
rs:fixedlength=\"true\"/>";
break;
case TypeCode.DateTime:
dt = "<s:datatype dt:type=\"dateTime\" rs:dbtype=\"timestamp\"
dt:maxLength=\"16\" rs:scale=\"0\" rsrecision=\"16\"
rs:fixedlength=\"true\"/>";
break;
case TypeCode.Boolean:
dt = "<s:datatype dt:type=\"boolean\" />";
break;
}
xmlstream.WriteText("<s:AttributeType name=\"" +
col.ColumnName +
"\" rs:number=\"" +
(col.Ordinal + 1).ToString()+
"\" rs:nullable=\"true\" rs:write=\"true\">" +
dt + "</s:AttributeType>", StreamWriteEnum.stWriteChar);

}

//End Schema
xmlstream.WriteText("<s:extends type=\"rs:rowbase\"
/></s:ElementType></s:Schema>",StreamWriteEnum.stWriteChar);

//Begin Rowset
xmlstream.WriteText("<rs:data><rs:insert>",StreamWriteEnum.stWriteChar);
//Add Rows

foreach(DataRow row in ds.Tables[0].Rows)
{
xmlstream.WriteText("<z:row",StreamWriteEnum.stWriteChar);
foreach (DataColumn col in row.Table.Columns)
{
xmlstream.WriteText(
" " + col.ColumnName + "=\""
+ System.Web.HttpUtility.HtmlEncode(row[col].ToString())
+ "\"",StreamWriteEnum.stWriteChar);
}
xmlstream.WriteText("/>",StreamWriteEnum.stWriteChar);
}
//End Rowset
xmlstream.WriteText("</rs:insert></rs:data></xml>",StreamWriteEnum.stWriteChar);

ADODB.Recordset rs = new ADODB.Recordset();

xmlstream.Position = 0;

rs.Open(xmlstream, Missing.Value,
CursorTypeEnum.adOpenUnspecified,LockTypeEnum.adLockUnspecified,0);

return rs;
}

public DataSet GetDataSet(ADODB.Recordset rs)
{
DataSet ds = new DataSet();

System.Data.OleDb.OleDbDataAdapter da = new
System.Data.OleDb.OleDbDataAdapter();
da.Fill(ds.Tables.Add(), rs);
return ds;
}
//GetDataSet(TempTable)
//GetTempTable(DataSet)
}
}
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert Dataset to Recordset in C# =?Utf-8?B?TWFyYXRob25lcg==?= Microsoft C# .NET 9 30th Dec 2004 07:39 PM
how to convert an ado recordset xml format to an ado.net dataset xml afsheen Microsoft ADO .NET 1 17th Mar 2004 12:04 PM
How can I convert an ADO.Net Dataset into a Classic ADO Recordset? Phil Agee Microsoft ADO .NET 4 2nd Dec 2003 04:22 PM
Convert ADODB recordset into .Net dataset Tor Christian Microsoft ADO .NET 2 19th Sep 2003 02:37 PM
Convert Dataset to ADODB.Recordset luis ugaz Microsoft ADO .NET 0 15th Sep 2003 04:50 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:13 AM.