zoukankan      html  css  js  c++  java
  • C#调用SAP函数,传入参数为表

    //using SAP.Middleware.Connector;
     
    //private RfcConfigParameters _params = new RfcConfigParameters();
    //private RfcDestination _rfcDest;
     
    //RfcConfigParameters parms = new RfcConfigParameters();
    //            parms.Add(RfcConfigParameters.Name, "xxx");
    //            parms.Add(RfcConfigParameters.AppServerHost, "xxx");
    //            parms.Add(RfcConfigParameters.SystemNumber, "00");
    //            parms.Add(RfcConfigParameters.Client, "900");
    //            parms.Add(RfcConfigParameters.User, "xxx");
    //            parms.Add(RfcConfigParameters.Password, "xxxxx");
    //            parms.Add(RfcConfigParameters.Language, "EN");
    //            parms.Add(RfcConfigParameters.PoolSize, "5");
    //            parms.Add(RfcConfigParameters.MaxPoolSize, "10");
    //            parms.Add(RfcConfigParameters.IdleTimeout, "600");
    //            return parms;
     
    //_rfcDest = RfcDestinationManager.GetDestination(_params);
    public DataTable GetSAPData(DataTable dt)
            {
                DataTable dtSAPData = new DataTable();
                try
                {
                    dtSAPData.Columns.Add("DATA1", typeof(string));
                    dtSAPData.Columns.Add("DATA2", typeof(string));
                    dtSAPData.Columns.Add("DATA3", typeof(string));
     
                    string strOrder = null;
     
                    RfcRepository   rfcRep      =   _rfcDest.Repository;
                    IRfcFunction    func        =   rfcRep.CreateFunction("Z_RFC_XXXX");
                    IRfcTable       tSAP    =   func.GetTable("INPUT_TABLE");
     
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        str1 = dt.Rows[i][0].ToString();
                        str2 = dt.Rows[i][1].ToString();
                        str3 = dt.Rows[i][2].ToString();
                        IRfcStructure struSAP = tSAP.Metadata.LineType.CreateStructure();
                        struSAP.SetValue("str1", str1);
                        struSAP.SetValue("str2", str2);
                        struSAP.SetValue("str3", str3);
                        tSAP.Append(struSAP);
                    }
                    func.SetValue("INPUT_TABLE", tSAP); //table 参数
                    func.SetValue("WERKS", "A");        //单个参数    
                    func.SetValue("STATUS", "B");    //单个参数
                    func.Invoke(_rfcDest);
                    IRfcTable SAPDataTable = func.GetTable("RETURN_TABLE");
     
                    for (int i = 0; i < SAPDataTable.RowCount; i++)
                    {
                        IRfcStructure   stru    =   SAPDataTable[i];
                        DataRow         dr      =   dtSAPData.NewRow();
                        dr["DATA1"] = stru.GetValue("X").ToString();   
                        dr["DATA2"] = stru.GetValue("Y").ToString();   
                        dr["DATA3"] = stru.GetValue("Z").ToString();   
                        dtSAPData.Rows.Add(dr);
                    }
                }
                catch(Exception ex)
                {
                    string strcatch = ex.Message;
                }
                return dtSAPData;
            }
            以上是传入传出都为table的情况。
            还有一种直接传入一条数据的情况,当然,SAP函数里面的接收参数还是table        
            代码如下:
           
            var rfcDestination = SAPUtility.GetDestination(SapDestionation.PRD_800, SapUsers.PRD_IT06);
            BAPI_USER_GETLIST
            RfcRepository rfcrep = rfcDestination.Repository;
            IRfcFunction myfun = rfcrep.CreateFunction("Z_MISGETTEXT");//调用的SAP函数
            var table = myfun.GetTable("ZOBJTEXT");//SAP函数的table
            //设置传入参数
            table.Insert();
            table.CurrentIndex = 0;
            var currentRow = table.CurrentRow;
            currentRow.SetValue("TDOBJECT", "测试OBJ");
            currentRow.SetValue("TDNAME", "测试NAME");
            currentRow.SetValue("TDID", "测试ID");
            currentRow.SetValue("LANGUAGE", "测试语言");
            myfun.Invoke(rfcDestination); //提交调用BAPI
            //取得返回值,这里的TEXT项目也是SAP函数table【ZOBJTEXT】中的字段
            for (int i = 0; i < table.RowCount; i++)
            {
                table.CurrentIndex = i;
                var currentRow2 = table.CurrentRow;
                Console.WriteLine(currentRow2.GetString("TEXT"));
            }
            Console.Read();
         
  • 相关阅读:
    postgresql 添加触发器
    postgresql 操作
    postgresql 更新自增变量
    java8 stream api 文件流甚是牛逼
    fastjson妙用
    idea中springboot内置tomcat控制台中文乱码解决
    linux中开放某个端口
    idea中application.properties文件防止中文汉字自动转换成unicode编码解决办法
    使用vue开源项目vue-framework-wz遇到的问题以及解决方案
    rsync的使用
  • 原文地址:https://www.cnblogs.com/mikemao/p/15736517.html
Copyright © 2011-2022 走看看