C# .NET - hi all - Asked By srikanth panneer selvam on 06-Oct-08 05:30 AM


i given a sample code here...


tempcoa = ((SAPbobsCOM.BusinessPartners)(tempcomp.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oBusinessPartners)));

temprecset = ((SAPbobsCOM.Recordset)(tempcomp.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)));

temprecset.DoQuery("select CardCode,CardName from OCRD where CardType ='S'");

tempcoa.Browser.Recordset = temprecset;


this tempreset is a recordset ..

i want to convert this to be datatable?



using sql2005

convert recordset to datatable and voice versa - Web Star replied to srikanth panneer selvam on 06-Oct-08 05:55 AM

static public ADODB.Recordset ConvertToRecordset(DataTable inTable)
{
    ADODB.Recordset result = new ADODB.Recordset();
    result.CursorLocation = ADODB.CursorLocationEnum.adUseClient;

    ADODB.Fields resultFields = result.Fields;
    System.Data.DataColumnCollection inColumns = inTable.Columns;

    foreach (DataColumn inColumn in inColumns)
    {
        resultFields.Append(inColumn.ColumnName
            , TranslateType(inColumn.DataType)
            , inColumn.MaxLength
            , inColumn.AllowDBNull ? ADODB.FieldAttributeEnum.adFldIsNullable : 
                                     ADODB.FieldAttributeEnum.adFldUnspecified
            , null);
    }

    result.Open(System.Reflection.Missing.Value
            , System.Reflection.Missing.Value
            , ADODB.CursorTypeEnum.adOpenStatic
            , ADODB.LockTypeEnum.adLockOptimistic, 0);

    foreach (DataRow dr in inTable.Rows)
    {
        result.AddNew(System.Reflection.Missing.Value, 
                      System.Reflection.Missing.Value);

        for (int columnIndex = 0; columnIndex < inColumns.Count; columnIndex++)
        {
            resultFields[columnIndex].Value = dr[columnIndex];
        }
    }

    return result;
}

A helper function (which can easily be extended) maps the .NET data types to the correct ADODB field type enumeration.

Collapse
static ADODB.DataTypeEnum TranslateType(Type columnType)
{
    switch (columnType.UnderlyingSystemType.ToString())
    {
        case "System.Boolean":
            return ADODB.DataTypeEnum.adBoolean;

        case "System.Byte":
            return ADODB.DataTypeEnum.adUnsignedTinyInt;

        case "System.Char":
            return ADODB.DataTypeEnum.adChar;

        case "System.DateTime":
            return ADODB.DataTypeEnum.adDate;

        case "System.Decimal":
            return ADODB.DataTypeEnum.adCurrency;

        case "System.Double":
            return ADODB.DataTypeEnum.adDouble;

        case "System.Int16":
            return ADODB.DataTypeEnum.adSmallInt;

        case "System.Int32":
            return ADODB.DataTypeEnum.adInteger;

        case "System.Int64":
            return ADODB.DataTypeEnum.adBigInt;

        case "System.SByte":
            return ADODB.DataTypeEnum.adTinyInt;

        case "System.Single":
            return ADODB.DataTypeEnum.adSingle;

        case "System.UInt16":
            return ADODB.DataTypeEnum.adUnsignedSmallInt;

        case "System.UInt32":
            return ADODB.DataTypeEnum.adUnsignedInt;

        case "System.UInt64":
            return ADODB.DataTypeEnum.adUnsignedBigInt;

        case "System.String":
        default:
            return ADODB.DataTypeEnum.adVarChar;
    }
}

check this one - C_A P replied to srikanth panneer selvam on 06-Oct-08 06:20 AM

Converting DataSet to ADODB.Recordset
See the code download for Dino Esposito's "Applied XML Programming for Microsoft .NET"
http://www.microsoft.com/MSPress/books/6235.asp
In chapter 4 there is written as an example an XmlRecordsetWriter class (custom XML writer) which is capable to write ADO recordset compliant XML. It has WriteRecordset method which can take a DataSet, DataTable or DataView and write it as Recordset.

check this code - C_A P replied to srikanth panneer selvam on 06-Oct-08 06:20 AM

Converting ADO Recordset to Dataset you can convert an ADO Recordset into an ADO.NET Dataset. You can use the OleDbDataAdapter to get rows from an ADODB.Recordset and the ADODB.Record and put them in a DataSet. The following C# code using System.Data.OleDb namespace will accomplish this.
   {
//Code creates an ADODB Recordset using the Northwind.mdb
database.
//You will need to change the path to point to the database on
your system.
//You could also be using a recordset returned from another
location or Component

ADODB.Recordset MyRs= new ADODB.Recordset();
ADODB.Connection MyCn = new ADODB.Connection();

MyCn.Open("Provider=Microsoft.Jet.OLEDB.4.0;data
source=D:\\Northwind.mdb","Admin","",0);
MyRs.Open("Select * from
Customers",MyCn,ADODB.CursorTypeEnum.adOpenStatic,
ADODB.LockTypeEnum.adLockOptimistic,0);

//Create and fill the dataset from the recordset and populate
grid from Dataset.
OleDbDataAdapter myDA = new OleDbDataAdapter();
DataSet myDS = new DataSet("MyTable");
myDA.Fill(myDS, MyRs, "MyTable");

dataGrid1.DataSource = myDS;
dataGrid1.DataMember="MyTAble";

//Close and destroy ADODB Objects
//Note that the OleDbDataAdapter.Fill overload that takes
//a DataTable and an ADO object implicitly calls Close on
//the ADO object when the Fill operation is complete.

MyCn.Close();
}
question?? - Perry replied to srikanth panneer selvam on 06-Oct-08 07:05 AM

Do you want new code or want to modify given code. what is

tempcoa = ((SAPbobsCOM.BusinessPartners)(tempcomp.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oBusinessPartners)));

Regards,

Megha