469,602 Members | 1,932 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

convert recordset to dataset

//Thaddeus Jacobs, MCP
//Kinematic Automation, Inc.
//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
//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
//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
//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,
"", "" );

//Begin Schema
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 = "";
case TypeCode.String:
dt = "<s:datatype dt:type=\"string\" dt:maxLength=\"1024\"
rs:precision=\"0\" rs:fixedlength=\"true\" />";
case TypeCode.Int16:
dt = "<s:datatype dt:type=\"int\" dt:maxLength=\"4\"
rs:precision=\"10\" rs:fixedlength=\"true\"/>";
case TypeCode.Int32:
dt = "<s:datatype dt:type=\"int\" dt:maxLength=\"4\"
rs:precision=\"10\" rs:fixedlength=\"true\"/>";
case TypeCode.Int64:
dt = "<s:datatype dt:type=\"int\" dt:maxLength=\"4\"
rs:precision=\"10\" rs:fixedlength=\"true\"/>";
case TypeCode.Decimal:
dt = "<s:datatype dt:type=\"float\" rs:precision=\"15\"
case TypeCode.Double:
dt = "<s:datatype dt:type=\"float\" rs:precision=\"15\"
case TypeCode.DateTime:
dt = "<s:datatype dt:type=\"dateTime\" rs:dbtype=\"timestamp\"
dt:maxLength=\"16\" rs:scale=\"0\" rs:precision=\"16\"
case TypeCode.Boolean:
dt = "<s:datatype dt:type=\"boolean\" />";
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\"

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

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

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

xmlstream.Position = 0;

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

return rs;

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

System.Data.OleDb.OleDbDataAdapter da = new
da.Fill(ds.Tables.Add(), rs);
return ds;
Nov 15 '05 #1
0 9366

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by jk | last post: by
9 posts views Thread by Marathoner | last post: by
9 posts views Thread by B. Salmon | last post: by
5 posts views Thread by slowmotiongenius | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.