1. 程式人生 > >C#連線SAP互動資料【開發】【部署】

C#連線SAP互動資料【開發】【部署】

【背景】

  在專案開發中經常會跟SAP互動資料【報工、移庫、收貨、發貨等等】需求,都要將業務資料及時同步到SAP系統中

【開發環境】

  .NET4.0+、Windows(x86)

  【第一步】

    從SAP官網下載.net連線SAP類庫:

  【第二步】

    將下載下來的類庫檔案:引用到專案中sapnco.dll,sapnco_utils.dll

   web.config檔案中新增以下配置,SAP的連線資訊

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <sectionGroup name="SAP.Middleware.Connector">
      <sectionGroup name="ClientSettings">
        <section name="DestinationConfiguration" type="SAP.Middleware.Connector.RfcDestinationConfiguration, sapnco"/>
      </sectionGroup>
    </sectionGroup>
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
  </startup>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0"/>
  </startup>
  <SAP.Middleware.Connector>
    <ClientSettings>
      <DestinationConfiguration>
        <destinations>
          <add NAME="IDE" USER="xxx" PASSWD="xxx" CLIENT="800" LANG="ZH" ASHOST="100.100.80.0" SYSNR="00" MAX_POOL_SIZE="10" IDLE_TIMEOUT="10"/>
        </destinations>
      </DestinationConfiguration>
    </ClientSettings>
  </SAP.Middleware.Connector>
</configuration>

【測試類】測試配置的引數是否能夠正常連線SAP

using SAP.Middleware.Connector;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestConnectSap
{
    public class Program
    {
        public static void Main(string[] args)
        {
            TestSAP();
        }

        public static void TestSAP()
        {
            Console.WriteLine("start test connection sap......");
            try
            {

                RfcDestinationManager.RegisterDestinationConfiguration(new DestinationConfig());
                RfcDestination sapRfcDest = RfcDestinationManager.GetDestination("DEV");
                RfcRepository rfcRep = sapRfcDest.Repository;
                sapRfcDest.Ping();
                Console.WriteLine("connection sap success!");
                Console.ReadLine();
            }
            catch (RfcLogonException ex)
            {

                Console.WriteLine("RfcLogonException:" + ex.StackTrace);
                Console.WriteLine("connect sap occurs exception,", ex.Message);


            }

            catch (RfcCommunicationException ex)
            {

                Console.WriteLine("RfcCommunicationException:" + ex.StackTrace);
                Console.WriteLine("connect sap occurs exception,", ex.Message);

            }

            catch (RfcAbapRuntimeException ex)
            {


                Console.WriteLine("RfcAbapRuntimeException:" + ex.StackTrace);
                Console.WriteLine("connect sap occurs exception,", ex.Message);

            }

            catch (RfcAbapBaseException ex)
            {

                Console.WriteLine("RfcAbapBaseException:" + ex.StackTrace);
                Console.WriteLine("connect sap occurs exception,", ex.Message);

            }

            catch (RfcInvalidStateException ex)
            {

                Console.WriteLine("RfcInvalidStateException:" + ex.StackTrace);
                Console.WriteLine("connect sap occurs exception,", ex.Message);

            }

            catch (NullReferenceException ex)
            {

                Console.WriteLine("NullReferenceException:" + ex.StackTrace);
                Console.WriteLine("connect sap occurs exception,", ex.Message);

            }

            catch (IndexOutOfRangeException ex)
            {
                Console.WriteLine("IndexOutOfRangeException:" + ex.StackTrace);
                Console.WriteLine("connect sap occurs exception,", ex.Message);


            }
            catch (Exception ex)
            {
                Console.WriteLine("StackInfo:" + ex.StackTrace);
                Console.WriteLine("connect sap occurs exception,", ex.Message);
            }
            Console.ReadLine();

        }
    }
}

【部署】

  不同位數的作業系統需要匹配不同的元件:sapnco.dll sapnco_utils.dll

  1. 【exe程式】.net2.0 安裝vc++2005  .net4.0 安裝vc++2010 引用相應的dll元件
  2. 【web】引用相應的dll元件