A
alexttp
My WebService API needs to return a RecordSet.
I know it's impossible (since the ADODB RecordSet is not
serializable), and thus I return an XmlDocument representing it.
Actually, the flow is as follows:
1. Given an SQL statement, it is executed on the DB, resulting in a
DataSet.
2. This DataSet is then used to populate an ADODB RecordSet in the
following manner:
private static Recordset CreateADODBRecordSet(DataTable i_oDataTable)
{
Recordset oRS = new Recordset();
try
{
//Loop through each column in the Dataset
foreach(DataColumn oColumn in i_oDataTable.Columns)
{
//Create the Field Types for the recordset
oRS.Fields.Append(oColumn.ColumnName,
GetADOType(oColumn.DataType.ToString()),
GetADOTypeSize(oColumn.DataType.ToString)),
FieldAttributeEnum.adFldIsNullable,
System.Reflection.Missing.Value);
}
//Open the recordset
oRS.Open(System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
CursorTypeEnum.adOpenKeyset,
LockTypeEnum.adLockOptimistic, 1);
//Loop through the table and fill the ADO Recordset
for(int i = 0; i < i_oDataTable.Rows.Count; i ++)
{
oRS.AddNew(System.Reflection.Missing.Value,
System.Reflection.Missing.Value);
//Loop through each column
for(int j = 0; j < i_oDataTable.Columns.Count; j ++)
{
oRS.Fields[j].Value = i_oDataTable.Rows[j];
}
oRS.Update(System.Reflection.Missing.Value,
System.Reflection.Missing.Value);
}
if(oRS.RecordCount > 0)
{
//Move to the first record
oRS.MoveFirst();
}
return oRS;
}
catch(System.Exception ex)
{
return null;
}
}
3. This RS is then converted into an XmlDocument:
private static XmlDocument ConvertADODBRecordset2XmlDocument(
ADODB.Recordset i_oRS)
{
XmlDocument oRes = null;
MSXML2.DOMDocument40 oDomDoc = new MSXML2.DOMDocument40Class();
i_oRS.Save(oDomDoc, ADODB.PersistFormatEnum.adPersistXML);
oRes = new XmlDocument();
oRes.LoadXml(oDomDoc.xml);
return oRes;
}
4. So far, all is well until... we try to put some unprintable
characters in one of the string fields! I mean, say, ASCII 31 and
smaller...
This time the i_oRS.Save(oDomDoc,
ADODB.PersistFormatEnum.adPersistXML) produces oDomDoc with an empty
XML in it... Naturally, converting it into XmlDocuments makes no
good... :-(
SO, THE QUESTION IS: what would you recommend to overcome this
problem?
Eagerly waiting for any suggestions... Other ways of converting
DataSet to RS are also accepted ;-)
Alex
I know it's impossible (since the ADODB RecordSet is not
serializable), and thus I return an XmlDocument representing it.
Actually, the flow is as follows:
1. Given an SQL statement, it is executed on the DB, resulting in a
DataSet.
2. This DataSet is then used to populate an ADODB RecordSet in the
following manner:
private static Recordset CreateADODBRecordSet(DataTable i_oDataTable)
{
Recordset oRS = new Recordset();
try
{
//Loop through each column in the Dataset
foreach(DataColumn oColumn in i_oDataTable.Columns)
{
//Create the Field Types for the recordset
oRS.Fields.Append(oColumn.ColumnName,
GetADOType(oColumn.DataType.ToString()),
GetADOTypeSize(oColumn.DataType.ToString)),
FieldAttributeEnum.adFldIsNullable,
System.Reflection.Missing.Value);
}
//Open the recordset
oRS.Open(System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
CursorTypeEnum.adOpenKeyset,
LockTypeEnum.adLockOptimistic, 1);
//Loop through the table and fill the ADO Recordset
for(int i = 0; i < i_oDataTable.Rows.Count; i ++)
{
oRS.AddNew(System.Reflection.Missing.Value,
System.Reflection.Missing.Value);
//Loop through each column
for(int j = 0; j < i_oDataTable.Columns.Count; j ++)
{
oRS.Fields[j].Value = i_oDataTable.Rows[j];
}
oRS.Update(System.Reflection.Missing.Value,
System.Reflection.Missing.Value);
}
if(oRS.RecordCount > 0)
{
//Move to the first record
oRS.MoveFirst();
}
return oRS;
}
catch(System.Exception ex)
{
return null;
}
}
3. This RS is then converted into an XmlDocument:
private static XmlDocument ConvertADODBRecordset2XmlDocument(
ADODB.Recordset i_oRS)
{
XmlDocument oRes = null;
MSXML2.DOMDocument40 oDomDoc = new MSXML2.DOMDocument40Class();
i_oRS.Save(oDomDoc, ADODB.PersistFormatEnum.adPersistXML);
oRes = new XmlDocument();
oRes.LoadXml(oDomDoc.xml);
return oRes;
}
4. So far, all is well until... we try to put some unprintable
characters in one of the string fields! I mean, say, ASCII 31 and
smaller...
This time the i_oRS.Save(oDomDoc,
ADODB.PersistFormatEnum.adPersistXML) produces oDomDoc with an empty
XML in it... Naturally, converting it into XmlDocuments makes no
good... :-(
SO, THE QUESTION IS: what would you recommend to overcome this
problem?
Eagerly waiting for any suggestions... Other ways of converting
DataSet to RS are also accepted ;-)
Alex