Step by step tutorial to use SAP. net Connector with VS 2008 [closed]

回眸只為那壹抹淺笑 提交于 2019-11-27 04:11:03

问题


I have found many example on the Internet but many are old (VS 2003, SAP. Net Connector 2.0 where the current is 3.0).

I wonder how to use SAP. NET Connector.

What files should I install?

References which add to the project?

How to add the provider to create a connection in Server Explorer?


回答1:


An example using vs2010 and SAP .NET Connector 3.0

Requirements:

  • Visual Studio .NET 2010 (the free c# Express edition is fine)
  • SAP .NET Connector 3.0 (you can usually obtain it from your client for the project at hand)

Installation

Identify the correct version for your platform. You might have something along the following:

.. for a Windows 7 64-bit installation, we'll use sapnco30dotnet40P_12-20007348.zip.

Expand the archive, and start the .msi installation file.

.. follow the installation procedure, leaving all default options (next.. next.. next.. finish).

Referencing

Open Visual Studio and create a new project (or open your own).

In the Solution Explorer panel, usually to your right, right-click on References and choose Add Reference:

.. then, select the Browse tab, navigate to the SAP Connector installation folder, and select both sapnco.dll and sapnco_utils.dll :

Now that you've correctly referenced the SAP .NET Connector in your Visual Studio project, you can write code for it.

Connecting

Start by creating a destination configuration class (replace the connection parameters with the ones you're given by your client):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAP.Middleware.Connector; // your sap connector

namespace WindowsFormsSapApplication1
{
    public class ECCDestinationConfig : IDestinationConfiguration
    {

        public bool ChangeEventsSupported()
        {
            return false;
        }

        public event RfcDestinationManager.ConfigurationChangeHandler ConfigurationChanged;

        public RfcConfigParameters GetParameters(string destinationName)
        {

            RfcConfigParameters parms = new RfcConfigParameters();

            if (destinationName.Equals("mySAPdestination"))
            {
                parms.Add(RfcConfigParameters.AppServerHost, "sapnode.mycompany.net");
                parms.Add(RfcConfigParameters.SystemNumber, "21");
                parms.Add(RfcConfigParameters.SystemID, "CF1");
                parms.Add(RfcConfigParameters.User, "mySAPuser");
                parms.Add(RfcConfigParameters.Password, "mySAPpassword");
                parms.Add(RfcConfigParameters.Client, "100");
                parms.Add(RfcConfigParameters.Language, "EN"); 
                parms.Add(RfcConfigParameters.PoolSize, "5");
            }
            return parms;

        }
    }
}

.. then, connect to SAP and call a function. Suppose that you want to retrieve the list of companies, using the BAPI_COMPANYCODE_GETLIST SAP function:

    public void GetCompanies() {

        ECCDestinationConfig cfg = new ECCDestinationConfig();

        RfcDestinationManager.RegisterDestinationConfiguration(cfg);

        RfcDestination dest = RfcDestinationManager.GetDestination("mySAPdestination");

        RfcRepository repo = dest.Repository;

        IRfcFunction testfn = repo.CreateFunction("BAPI_COMPANYCODE_GETLIST");

        testfn.Invoke(dest);

        var companyCodeList = testfn.GetTable("COMPANYCODE_LIST");

        // companyCodeList now contains a table with companies and codes

    }

Making life easier

In the above example, the GetTable function returns a SAP table which you may like, or not. There's a handy extension that transforms that table in a familiar .NET DataTable, as follows:

public static class IRfcTableExtentions
{
    /// <summary>
    /// Converts SAP table to .NET DataTable table
    /// </summary>
    /// <param name="sapTable">The SAP table to convert.</param>
    /// <returns></returns>
    public static DataTable ToDataTable(this IRfcTable sapTable, string name)
    {
        DataTable adoTable = new DataTable(name);
        //... Create ADO.Net table.
        for (int liElement = 0; liElement < sapTable.ElementCount; liElement++)
        {
            RfcElementMetadata metadata = sapTable.GetElementMetadata(liElement);
            adoTable.Columns.Add(metadata.Name, GetDataType(metadata.DataType));
        }

        //Transfer rows from SAP Table ADO.Net table.
        foreach (IRfcStructure row in sapTable)
        {
            DataRow ldr = adoTable.NewRow();
            for (int liElement = 0; liElement < sapTable.ElementCount; liElement++)
            {
                RfcElementMetadata metadata = sapTable.GetElementMetadata(liElement);

                switch (metadata.DataType)
                {
                    case RfcDataType.DATE:
                        ldr[metadata.Name] = row.GetString(metadata.Name).Substring(0, 4) + row.GetString(metadata.Name).Substring(5, 2) + row.GetString(metadata.Name).Substring(8, 2);
                        break;
                    case RfcDataType.BCD:
                        ldr[metadata.Name] = row.GetDecimal(metadata.Name);
                        break;
                    case RfcDataType.CHAR:
                        ldr[metadata.Name] = row.GetString(metadata.Name);
                        break;
                    case RfcDataType.STRING:
                        ldr[metadata.Name] = row.GetString(metadata.Name);
                        break;
                    case RfcDataType.INT2:
                        ldr[metadata.Name] = row.GetInt(metadata.Name);
                        break;
                    case RfcDataType.INT4:
                        ldr[metadata.Name] = row.GetInt(metadata.Name);
                        break;
                    case RfcDataType.FLOAT:
                        ldr[metadata.Name] = row.GetDouble(metadata.Name);
                        break;
                    default:
                        ldr[metadata.Name] = row.GetString(metadata.Name);
                        break;
                }
            }
            adoTable.Rows.Add(ldr);
        }
        return adoTable;
    }

    private static Type GetDataType(RfcDataType rfcDataType)
    {
        switch (rfcDataType)
        {
            case RfcDataType.DATE:
                return typeof(string);
            case RfcDataType.CHAR:
                return typeof(string);
            case RfcDataType.STRING:
                return typeof(string);
            case RfcDataType.BCD:
                return typeof(decimal);
            case RfcDataType.INT2:
                return typeof(int);
            case RfcDataType.INT4:
                return typeof(int);
            case RfcDataType.FLOAT:
                return typeof(double);
            default:
                return typeof(string);
        }
    }
}

Reference: http://antswift.wordpress.com/2011/12/22/irfctable-to-net-datatable-extention-method/

After adding the above extension, you can now transform the result into a DataTable:

// get the regular SAP structured table..
var companyCodeList = testfn.GetTable("COMPANYCODE_LIST");

// turn it into a DataTable..
var companyDataTable = companyCodeList.ToDataTable();

// use it
SomeForm.DataGridView.DataSource=companyDataTable;

Notes

  • While this example is based on vs2010, other Visual Studio editions should run just fine.
  • It appears that SAP function names in the Connector should be always UPPERCASE.
  • If you get the SAP Connector architecture wrong, e.g. you install the 64bit version and then try to compile for 32-bit x86, you will get an error like: "Could not load file or assembly 'sapnco, Version=3.0.0.42, Culture=neutral, PublicKeyToken=50436dca5c7f7d23' or one of its dependencies. An attempt was made to load a program with an incorrect format." This is normal; just install the correct version.

Additional Reading

  • http://service.sap.com/connectors
  • http://antswift.wordpress.com/2011/12/22/connecting-to-sap-and-retrieving-data-using-version-3-0-of-the-sap-net-connector/
  • http://www.codeproject.com/Tips/521485/SAP-Integration-with-Net-4-0-SAP-Connection-Manage
  • http://www.dataxstream.com/2011/11/nco-3-rfc-client-vb-net-step-by-step/


来源:https://stackoverflow.com/questions/5300049/step-by-step-tutorial-to-use-sap-net-connector-with-vs-2008

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!