Question

Recently i am working on a project that required to fetch the data from SAP table to MICROSOFT SQL table ,I have completed the connection part and i also pulled the SAP structure values into ASP.NET.

My question is how to save the structure into Sql table ?

here is my code ;

RfcDestination mydestination = RfcDestinationManager.GetDestination("rfcecp");
        RfcRepository myrepository = mydestination.Repository;
        IRfcFunction EHSIMSFM = myrepository.CreateFunction("ZEHSIMS");

        EHSIMSFM.Invoke(mydestination);

        IRfcTable positable = EHSIMSFM.GetTable("POSITIONTAB1");

        if (positable.RowCount > 0)"THIS IS THE SAP STRUCTURE WITH ROW COUNT '300'."
        {

            posid.Text = "working";
        }
        else
        {

            posid.Text = "notworking";
        }
    }
    catch (Exception ej)
    {
        posid.Text = ej.Message;
    }
Was it helpful?

Solution

You can gather all the information from a returning IRfcTable via NCo 3 SAP Connector.

Here is the way to traverse each row (IRfcStructure) from the IRfcTable object..

foreach (IRfcStructure row in rfcTable)
{
    for (int element = 0; element < rfcTable.ElementCount; element++)
    {
        RfcElementMetadata metadata = rfcTable.GetElementMetadata(element);
        // row.GetString(metadata.Name); // get value from row's field
    }

    // You have your data from a row here..
}


Example: Here is the code which converts IRfcTable into DataTable.

public static DataTable CreateDataTable(IRfcTable rfcTable)
{
    var dataTable = new DataTable();

    for (int element = 0; element < rfcTable.ElementCount; element++)
    {
        RfcElementMetadata metadata = rfcTable.GetElementMetadata(element);
        dataTable.Columns.Add(metadata.Name);
    }

    foreach (IRfcStructure row in rfcTable)
    {
        DataRow newRow = dataTable.NewRow();
        for (int element = 0; element < rfcTable.ElementCount; element++)
        {
            RfcElementMetadata metadata = rfcTable.GetElementMetadata(element);
            newRow[metadata.Name] = row.GetString(metadata.Name);
        }
        dataTable.Rows.Add(newRow);
    }

    return dataTable;
}

Similarly, you can add your data into MSSQL DB.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top