C#连接SAP交互数据【开发】【部署】

【背景】

  在项目开发中经常会跟SAP交互数据【报工、移库、收货、发货等等】需求,都要将业务数据及时同步到SAP系统中

【开发环境】

  .NET4.0+、Windows(x86)

  【第一步】

    从SAP官网下载.net连接SAP类库:

  【第二步】

    将下载下来的类库文件:引用到项目中sapnco.dll,sapnco_utils.dll

   web.config文件中添加以下配置,SAP的连接信息



  
    
      
        

【测试类】测试配置的参数是否能够正常连接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组件

你可能感兴趣的:(C#,sap,c#,sap)