By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,732 Members | 1,435 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,732 IT Pros & Developers. It's quick & easy.

convert recordset to dataset

P: n/a
//Author:
//Thaddeus Jacobs, MCP
//Kinematic Automation, Inc.
//mailto:tj*****@kinematic.com
//
//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\"
rs:precision=\"0\" rs:fixedlength=\"true\" />";
break;
case TypeCode.Int16:
dt = "<s:datatype dt:type=\"int\" dt:maxLength=\"4\"
rs:precision=\"10\" rs:fixedlength=\"true\"/>";
break;
case TypeCode.Int32:
dt = "<s:datatype dt:type=\"int\" dt:maxLength=\"4\"
rs:precision=\"10\" rs:fixedlength=\"true\"/>";
break;
case TypeCode.Int64:
dt = "<s:datatype dt:type=\"int\" dt:maxLength=\"4\"
rs:precision=\"10\" rs:fixedlength=\"true\"/>";
break;
case TypeCode.Decimal:
dt = "<s:datatype dt:type=\"float\" rs:precision=\"15\"
rs:fixedlength=\"true\"/>";
break;
case TypeCode.Double:
dt = "<s:datatype dt:type=\"float\" rs:precision=\"15\"
rs:fixedlength=\"true\"/>";
break;
case TypeCode.DateTime:
dt = "<s:datatype dt:type=\"dateTime\" rs:dbtype=\"timestamp\"
dt:maxLength=\"16\" rs:scale=\"0\" rs:precision=\"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>",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)
{
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.adLo ckUnspecified,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)
}
}
Nov 15 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.