Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Connect Office Apps to .NET with "Lite" Web Services


Connect Office Apps to .NET with "Lite" Web Services

Listing 1 ConvertDataTableToRecordset


// Converts the specified DataTable into an ADO Recordset.
static public ADODB.Recordset ConvertDataTableToRecordset(DataTable table) 
{
  // Create a new (disconnected) Recordset
  ADODB.Recordset recordset = new ADODB.Recordset();

  // Create an array for the names of the colums to use
  // later with the AddNew method of the Recordset.
  object [] fieldList = new object[table.Columns.Count];

  // Loop through all of the columns in the DataTable
  // and define each as a new field in the Recordset;
  // also add the name to the fieldList array.
  for (int i=0; i < table.Columns.Count; i++) 
  {
    // get the field name, or create one if necessary
    string fieldName = table.Columns[i].ColumnName;
    if ( fieldName == null || fieldName.Length == 0 )
      fieldName = string.Format("Column{0}", i);
    fieldList[i] = fieldName;
			
    // Lookup the field's equivalent ADO type and maximum size
    ADODB.DataTypeEnum adoType;
    int adoSize;
    GetADOType(table.Columns[i].DataType, out adoType, out adoSize);

    // add the field to the ADO Recordset
    recordset.Fields.Append(fieldName, adoType, adoSize,
      ADODB.FieldAttributeEnum.adFldIsNullable, null);

    // These generic values appear to work well with any size
    // decimal/numeric; setting them for other types does not
    // cause harm, but they are necessary for decimals.
    recordset.Fields[i].Precision = 0;
    recordset.Fields[i].NumericScale = 25; //the maximum
  } //for

  // Now that the fields are defined, open the Recordset so we may
  // add the data. Notice that because C# does not work like VB with
  // optional parameters, we use the special Missing object which
  // is defined in System.Reflection.
  recordset.Open(Missing.Value, Missing.Value,
    ADODB.CursorTypeEnum.adOpenUnspecified,
    ADODB.LockTypeEnum.adLockUnspecified, -1);

  for (int i=0; i < table.Rows.Count; i++ )
  {
    // get the current record's values into an array
    object [] values = table.Rows[i].ItemArray;

    // ADO does not recognize GUID, or unique identifiers,
    // unless they are wrapped in curly braces, we check for
    // any occurences of them here and convert them to a
    // string in the format "{00000000-0000-0000-0000-000000000000}"
    for (int j=0; j < values.Length; j++ )
      if ( values[j] is System.Guid )
        values[j] = '{' + values[i].ToString() + '}';

    // add the current record to the ADO Recordset
    recordset.AddNew(fieldList, values);
  }

  // Done - Recordset complete!
  return recordset;
}


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.