• ASP.NET连接SAP的实现下单的功能
  • 发布于 2个月前
  • 165 热度
    0 评论
  • 林顺忍
  • 3 粉丝 32 篇博客
  •   

最近开发的一个采购系统需要和公司内部的SAP系统进行数据的交互,简单点理解就是我们可以通过自己开发的系统把用户的请购需求在审批通过后可以直接往SAP下采购订单,所以需要实现.NET与SAP系统的交互,下面把这个过程简单记录一下,方便以后查阅。

第一步:配置webconfig

我们把SAP的服务器地址,链接的用户名,密码都配置在webconfig中,具体代码如下:


    <add key="SAPEnv" value="P01" />
    <add key="SAPServerHost" value="192.168.6.64" />
    <add key="SAPSystemNumber" value="31" />
    <add key="SAPUser" value="TestUser" />
    <add key="SAPPassword" value="TestPwd" />
    <add key="SAPClient" value="200" />
    <add key="SAPLanguage" value="ZH" />
    <add key="SAPPoolSize" value="5" />
    <add key="SAPMaxPoolSize" value="10" />
    <add key="SAPIdleTimeout" value="60" />
第二步:把连接SAP的登录方式写在一个类中,代码如下:



    public static class SAPRegisterBLL
    {
        public static void RegisterRFCDest()
        {
            IDestinationConfiguration ID = new MyBackendConfig();
            RfcDestinationManager.RegisterDestinationConfiguration(ID);
        }
        /// <summary>
        /// 登陆SAP前的准备工作
        /// </summary>
        public class MyBackendConfig : IDestinationConfiguration
        {
            public RfcConfigParameters GetParameters(String destinationName)
            {
                string sSAPServerHost = ConfigurationManager.AppSettings["SAPServerHost"] ?? "10.121.128.94";
                string sSAPSystemNumber = ConfigurationManager.AppSettings["SAPSystemNumber"] ?? "64";
                string sSAPUser = ConfigurationManager.AppSettings["SAPUser"] ?? "AVAILCHECK ";
                string sPassword = ConfigurationManager.AppSettings["SAPPassword"] ?? "JAVA";
                string sSAPClient = ConfigurationManager.AppSettings["SAPClient"] ?? "200";
                string sSAPLanguage = ConfigurationManager.AppSettings["SAPLanguage"] ?? "DE";
                string sSAPMaxPoolSize = ConfigurationManager.AppSettings["SAPMaxPoolSize"] ?? "5";


                RfcConfigParameters parms = new RfcConfigParameters();
                parms.Add(RfcConfigParameters.AppServerHost, sSAPServerHost);
                parms.Add(RfcConfigParameters.SystemNumber, sSAPSystemNumber);
                parms.Add(RfcConfigParameters.User, sSAPUser);
                parms.Add(RfcConfigParameters.Password, sPassword);
                parms.Add(RfcConfigParameters.Client, sSAPClient);
                parms.Add(RfcConfigParameters.Language, sSAPLanguage);
                //parms.Add(RfcConfigParameters.PoolSize, "5");
                parms.Add(RfcConfigParameters.PeakConnectionsLimit, sSAPMaxPoolSize);
                //parms.Add(RfcConfigParameters.IdleTimeout, "600");
                return parms;
            }
            public bool ChangeEventsSupported()
            {
                return false;
            }
            public event RfcDestinationManager.ConfigurationChangeHandler ConfigurationChanged;
        }
    }

第三步:在Global.asax文件中注册登录连接

protected void Application_Start(object sender, EventArgs e)
        {
            SAPRegisterBLL.RegisterRFCDest();
        }
第四步:通过以上步骤,就可以实现具体的逻辑了,我这边给一个从SAP系统获取数据的例子

   /// <summary>
        /// 从SAP获取数据
        /// </summary>
        /// <param name="customerID"></param>
        /// <param name="salesOrderNo"></param>
        /// <param name="itemNo"></param>
        /// <param name="customerInfo"></param>
        /// <param name="payNature"></param>
        /// <returns></returns>
        public static void SyncDataFromSAP(string date)
        {
            try
            {
                List<ImportDataFromSAP> list = new List<ImportDataFromSAP>();
                string sSAPEnv = ConfigurationManager.AppSettings["SAPEnv"] ?? "E01";
                RfcDestination dest = RfcDestinationManager.GetDestination(sSAPEnv);
                RfcRepository repo = dest.Repository;
                IRfcFunction salesOrderBapi = repo.CreateFunction("ZMM_SAP_K2_RFQ1");
                //set import parameter
                string syncSAPAttachFolder = ConfigurationManager.AppSettings["SyncSAPAttachFolder"];
                salesOrderBapi.SetValue("IV_AEDAT", date); //RFQ创建日期
                salesOrderBapi.SetValue("IV_PATH", @"/mnt/EBIDDING/");
                salesOrderBapi.SetValue("IV_SERV_PATH", date);
                salesOrderBapi.SetValue("IV_MODE", syncSAPAttachFolder);
                RfcSessionManager.BeginContext(dest);
                salesOrderBapi.Invoke(dest);
                RfcSessionManager.EndContext(dest);
                IRfcTable tblSOOperations = salesOrderBapi.GetTable("ET_RFQ");  //获取Operation
                if (tblSOOperations.Count > 0)
                {
                    for (int i = 0; i < tblSOOperations.Count; i++)
                    {
                        tblSOOperations.CurrentIndex = i;
                        ImportDataFromSAP item = new ImportDataFromSAP();
                        item.PRNO = tblSOOperations.GetString("BANFN");
                        item.PRLineNo = tblSOOperations.GetInt("BNFPO").ToString();
                        item.SupplierID = tblSOOperations.GetString("LIFNR");
                        item.RFQNO = tblSOOperations.GetString("EBELN");
                        item.LineItem = tblSOOperations.GetInt("EBELP").ToString();
                        item.RFQType = tblSOOperations.GetString("BSART");
                        item.MaterialCode = tblSOOperations.GetString("MATNR");
                        item.MaterialName = tblSOOperations.GetString("TXZ01");
                        item.RFQQTY = tblSOOperations.GetInt("KTMNG");
                        item.Unit = tblSOOperations.GetString("MEINS");
                        item.BuyerCode = tblSOOperations.GetString("EKGRP");
                        string _endDate = tblSOOperations.GetString("AGDAT");
                        if (!string.IsNullOrEmpty(_endDate))
                        {
                            try
                            {
                                item.EndDate = Convert.ToDateTime(_endDate.Trim());
                            }
                            catch (Exception ex)
                            {
                            }
                        }
                        string _deliveryDate = tblSOOperations.GetString("EINDT");
                        if (!string.IsNullOrEmpty(_deliveryDate))
                        {
                            try
                            {
                                item.DeliveryDate = Convert.ToDateTime(_deliveryDate.Trim());
                            }
                            catch (Exception ex)
                            {
                            }
                        }
                        item.NetPrice = tblSOOperations.GetDecimal("NETPR");
                        item.NetPriceUnit = tblSOOperations.GetString("PEINH");
                        item.FactoryCode = tblSOOperations.GetString("WERKS");
                        item.TrackingNo = tblSOOperations.GetString("BEDNR");
                        item.DrawingNo = tblSOOperations.GetString("DOKNR");
                        item.DrawingVersion = tblSOOperations.GetString("DOKVR");
                        item.FileAddress = tblSOOperations.GetString("EV_FILEPATH");
                        item.CreateAt = tblSOOperations.GetString("AEDAT");
                        item.CreateBy = tblSOOperations.GetString("ERNAM");
                        item.Engineer = tblSOOperations.GetString("ERNAM_TECH");
                        item.Tel = tblSOOperations.GetString("TEL");
                        item.SaveType = tblSOOperations.GetString("RAUBE");
                        item.SaveTypeDesc = tblSOOperations.GetString("RBTXT");
                        item.SyncTime = System.DateTime.Now;
                        item.IsActive = true;
                        list.Add(item);

                    }
                    if (list.Count > 0)
                    {
                        SAPBLL.ImportData(list);
                    }
                }
            }
            catch (Exception EX)
            {

            }
            finally
            {
            }
        
        }

备注:

.NET与SAP的连接交互需要用到sapnco.dll这个类库,大家可以去网上百度搜一下,下载后在项目中引用一下就OK了


用户评论