使用 SAP 的分步教程。 VS 2008 的网络连接器

发布于 2024-10-22 00:39:10 字数 1539 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

べ繥欢鉨o。 2024-10-29 00:39:10

使用 vs2010 和 SAP .NET Connector 3.0 的示例

要求:

  • Visual Studio .NET 2010(免费的 c# Express 版本即可)
  • SAP .NET Connector 3.0(您通常可以从您手头的项目的客户那里获取它)

安装

确定正确的 安装适合您平台的版本。您可能有以下内容:

.. 对于 Windows 7 64 位安装,我们将使用 sapnco30dotnet40P_12-20007348.zip

展开存档,然后启动 .msi 安装文件。

.. 按照安装过程进行操作,保留所有默认选项(下一步.. 下一步.. 下一步.. 完成)。

引用

打开 Visual Studio 并创建一个新项目(或打开您自己的项目)。

在“解决方案资源管理器”面板(通常位于右侧)中,右键单击“引用”并选择“添加引用”:

.. 然后,选择浏览选项卡,导航到 SAP Connector 安装文件夹,然后选择 sapnco.dllsapnco_utils.dll

现在您已经正确引用了 SAP .NET Connector 在您的 Visual Studio 项目中,您可以为其编写代码。

连接

首先创建目标配置类(将连接参数替换为客户端提供的连接参数):

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;

        }
    }
}

.. 然后,连接到 SAP 并调用函数。假设您想要使用 BAPI_COMPANYCODE_GETLIST SAP 函数检索公司列表:

    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

    }

让生活更轻松

在上面的示例中,GetTable 函数返回一个 SAP 您可能喜欢或不喜欢的表格。有一个方便的扩展可以在熟悉的 .NET DataTable 中转换该表,如下所示:

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);
        }
    }
}

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

添加上述扩展后,您现在可以将结果转换为 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;

注意

  • 虽然此示例基于 vs2010,但其他 Visual Studio 版本应该可以正常运行。
  • 连接器中的 SAP 函数名称似乎应始终大写。
  • 如果 SAP Connector 架构错误,例如安装 64 位版本,然后尝试编译 32 位 x86,您将收到如下错误:“无法加载文件或程序集 'sapnco,版本 = 3.0。 0.42、Culture=neutral、PublicKeyToken=50436dca5c7f7d23' 或其依赖项之一尝试加载格式不正确的程序。” 这是正常现象;只需安装正确的版本即可。

附加阅读

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文