首页 新闻 会员 周边 捐助

winform当中,调用SAP接口,查询,新增功能

1
[已关闭问题] 关闭于 2014-02-26 09:00

由于前段时间给XX集团改个项目,当中,要调用SAP接口,也网上找了些资料,慢慢地去试,问别人,爱理不理的,弄了的时间有点长,结果自己摸索出来了,在这边主要把功能解释下,并贴出代码,供需要的同行们参考。

一、封装SAP调用接口

1.添加引用SAP.Connector.dll,SAP.Connector.Rfc.dll

2.配置App.config

<appSettings>

<add key="Name" value=""/>
  <add key="AppServerHost" value="地址"/>
  <add key="SystemNumber" value="00"/>
  <add key="User" value="用户名"/>
  <add key="Password" value="密码"/>
  <add key="Client" value="510"/>
  <add key="Language" value="ZH"/>
 </appSettings>

   这样做,比较方便用户修改,也可做成可配置的

 

2.新建一个类"MyBackendConfig" ,并继承IDestinationConfiguration

  public class MyBackendConfig : IDestinationConfiguration
    {
        #region IDestinationConfiguration 成员

        public bool ChangeEventsSupported()
        {
            return false;
        }

        public event RfcDestinationManager.ConfigurationChangeHandler ConfigurationChanged;

        public RfcConfigParameters GetParameters(string destinationName)
        {
            string sName = ConfigurationManager.AppSettings["Name"].ToString();
            string sAppServerHost = ConfigurationManager.AppSettings["AppServerHost"].ToString();
            string sSystemNumber = ConfigurationManager.AppSettings["SystemNumber"].ToString();
            string sUser = ConfigurationManager.AppSettings["User"].ToString();
            string sPassword = ConfigurationManager.AppSettings["Password"].ToString();
            string sClient = ConfigurationManager.AppSettings["Client"].ToString();
            string sLanguage = ConfigurationManager.AppSettings["Language"].ToString();
          
            if (sName.Equals(destinationName))
            {
                RfcConfigParameters parms = new RfcConfigParameters();
                parms.Add(RfcConfigParameters.Name, sName);
                parms.Add(RfcConfigParameters.AppServerHost, sAppServerHost);
                parms.Add(RfcConfigParameters.SystemNumber, sSystemNumber);
                parms.Add(RfcConfigParameters.User, sUser);
                parms.Add(RfcConfigParameters.Password, sPassword);
                parms.Add(RfcConfigParameters.Client, sClient);
                parms.Add(RfcConfigParameters.Language, sLanguage);
                return parms;
            }
            else return null;
        }

        #endregion
    }

 

二、调用SAP接口

   /// <summary>
    /// SAP接口调用
    /// </summary>
    public class SAPService
    {

   public static string nco(string sPZV, string sPSN)
        {
            IDestinationConfiguration ID = new MyBackendConfig();
            RfcDestinationManager.RegisterDestinationConfiguration(ID);
            RfcDestination prd = RfcDestinationManager.GetDestination("");
            RfcDestinationManager.UnregisterDestinationConfiguration(ID);
            return nco(prd, sPZV, sPSN);
        }

       static string sMATNR = string.Empty;

///查询返回的值
        public static string nco(RfcDestination prd, string sPZV, string sPSN)
        {
            try
            {
                RfcRepository repo = prd.Repository;
                IRfcFunction companyBapi = repo.CreateFunction("ZPP_SERNR_GENERANT"); //ZPP_SERNR_GENERANT 整车车号
                companyBapi.SetValue("L_TEXT", sPZV);//设置导出参数
                companyBapi.SetValue("H_TEXT", sPSN);
                companyBapi.SetValue("NUMB", "");
                companyBapi.SetValue("TIME", "1");
                try
                {
                    companyBapi.Invoke(prd);
                }
                catch (Exception ex)
                {

                }
                IRfcTable table = companyBapi.GetTable("ZSERNR6");  //获取相应的品号内表
                DataTable dt = new DataTable();  //新建表格
                dt.Columns.Add("SERNR");  //表格添加一列
                for (int i = 0; i < table.RowCount; i++)
                {
                    table.CurrentIndex = i;  //当前内表的索引行
                    DataRow dr = dt.NewRow();
                    sMATNR = table.GetString("SERNR").ToString();  //获取表格的某行某列的值
                }
                prd = null;
                repo = null;
            }
            catch (Exception)
            {
                initSys.TS_MessageBox("网络故障!");
            }
            return sMATNR;
        }

///新增,通 SAP接口上传数据到服务器中

        public static string nco2(RfcDestination prd, string sPDM, string sPZV, string sFac, string sSN, string sStartTime, string sEndTime, string userid)
        {
            string a = string.Empty;
            string b = string.Empty;
            string c = string.Empty;
            string d = string.Empty;
            try
            {
                RfcRepository repo = prd.Repository;
                IRfcFunction companyBapi = repo.CreateFunction("ZDMS_CONFIG_UPDAT");//ZDMS_CONFIG_UPDAT整车档案装配    
                IRfcTable table = companyBapi.GetTable("GT_INPUT");  //获取相应的品号内表

                DataSet ds = initSys.ws.GetAssembly_tree_define(sPDM, sPZV, sFac, sSN, sStartTime, sEndTime);

                if (ds.Tables[0].Rows.Count > 0)
                {
                    // int iCount = ds.Tables[0].Rows.Count;
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        //iNum++;
                        count = 1;
                        sPDM = ds.Tables[0].Rows[i]["PPDM"].ToString().Trim();
                        sPZV = ds.Tables[0].Rows[i]["PPZV"].ToString().Trim();
                        sFac = ds.Tables[0].Rows[i]["PFac"].ToString().Trim();
                        sSN = ds.Tables[0].Rows[i]["PSN"].ToString().Trim();
                        sTo = ds.Tables[0].Rows[i]["PPDM"].ToString().Trim();

                        string sTest = nco(sPZV.Split('*')[0].ToString(), sSN);//整车车号

                        a = ds.Tables[0].Rows[i]["CPDM"].ToString().Trim();
                        b = ds.Tables[0].Rows[i]["CPZV"].ToString().Trim();
                        DataTable dt = initSys.ws.GetNewOldBarCode(ds.Tables[0].Rows[i]["CFac"].ToString()).Tables[0];
                        if (dt.Rows.Count > 0)
                        {
                            c = dt.Rows[0][0].ToString().Trim();
                        }
                        else
                        {
                            c = ds.Tables[0].Rows[i]["CFac"].ToString().Trim();
                        }

                        d = ds.Tables[0].Rows[i]["CSN"].ToString().Trim();
                        s1 = a;
                        s2 = d;

//下面这一段是递归读取本地数据,并通过 SAP接口上传

                                 NewMethod(sTest, sTo, sPDM, sPZV, sFac, sSN, sStartTime, sEndTime, a, b, ds.Tables[0].Rows[i]["CFac"].ToString(), d, table, userid);

             }

          companyBapi.SetValue("GT_INPUT", table);
                    try
                    {
                        companyBapi.Invoke(prd);
                    }
                    catch (Exception)
                    {

                    }
                    sMATNR = table.GetString("UPD_FLAG").ToString() + "|" + iNum.ToString();
                    prd = null;
                    repo = null;
                    iNum = 0;
                }
                return sMATNR;
            }
            catch (Exception)
            {
                iNum = 0;
                return "网络故障!";
            }
        }

}

主要是贴出代码,有些时候,会做了,就会知道,是什么问题,仅供参考,如有问题,可咨询

Soul 今天的主页 Soul 今天 | 菜鸟二级 | 园豆:206
提问于:2012-09-08 16:22
< >
分享
所有回答(1)
0

请问上面的两个两个SAP.Connector.dll,SAP.Connector.Rfc.dll那里找的,当初对接的时候SAP运营商会提供吗?

youran110 | 园豆:202 (菜鸟二级) | 2017-05-23 16:41
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册