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();

 

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值