RFC调用SAP接口程序之输入输出表格案例2

      c#接口调用RFC接口教程系列2,这个使用2.0接口。哈哈,天下武功就是在不断切磋中,锻炼出来的,技术也一样。

       

string str111 = "";     //物料文件號碼
        string str222 = "";     //年度
        IDestinationConfiguration ID = new RfcConfig();
        RfcDestinationManager.RegisterDestinationConfiguration(ID);
        RfcDestination prd = RfcDestinationManager.GetDestination("PRD_000");//正式区000环境
        RfcDestinationManager.UnregisterDestinationConfiguration(ID);
        RfcRepository repo = prd.Repository;
        IRfcFunction companyBapi = repo.CreateFunction("ZM_PURCHASE_GOOD01");   //调用函数名

        try
        {
            IRfcTable tableSAP = companyBapi.GetTable("IT_POGO");   //表体
            for (int i = 0; i < gvlist1.Rows.Count; i++)     //  1 sapdt.Rows.Count
            {
                // TextBox txtnum = (TextBox)gvlist1.Rows[i].FindControl("txtnum");
                HiddenField hfQYA24 = (HiddenField)gvlist1.Rows[i].FindControl("hfQYA24");

                IRfcStructure st = tableSAP.Metadata.LineType.CreateStructure();
                st.SetValue("EBELN", gvlist1.Rows[i].Cells[9].Text.Trim());           // 采购单单号      "3020092357"
                st.SetValue("EBELP", gvlist1.Rows[i].Cells[10].Text.Trim());               // 采购单项目单号码     "10"
                st.SetValue("MENGE", gvlist1.Rows[i].Cells[7].Text.Trim());        //采购单数量  "1"
                st.SetValue("REF_DOC_YR", lblQY23.Text.Trim());                    //sap收货单号
                st.SetValue("REF_DOC", lblQY22.Text.Trim());                       //sap收货年度
                st.SetValue("REF_DOC_IT", hfQYA24.Value);
                tableSAP.Append(st);
            }

            companyBapi.SetValue("I_BWART", "105");
            companyBapi.SetValue("I_BUDAT", DateTime.Now.ToString("yyyyMMdd"));
            companyBapi.SetValue("ZFORM", "800");
            companyBapi.SetValue("ZOABH", model.QY02);    //单号

            companyBapi.SetValue("IT_POGO", tableSAP);
            companyBapi.Invoke(prd);   //执行函数

            str111 = companyBapi.GetString("E_MBLNR");
            str222 = companyBapi.GetString("E_MJAHR");
            IRfcTable OPTIONS = companyBapi.GetTable("RETURN");    //返回表格值
            bool IsFail = false;
            StringBuilder strMsg = new StringBuilder();
            for (int i = 0; i < OPTIONS.RowCount; i++)
            {
                OPTIONS.CurrentIndex = i;
                if (OPTIONS.GetString("TYPE").Equals("E"))
                {
                    IsFail = true;
                    strMsg.Append(OPTIONS.GetString("MESSAGE"));
                    strMsg.Append("<br>");
                }
            }

            if (IsFail)
            {
                SystemTools.displayTips("SAP导入异常!具体明细:" + strMsg.ToString(), 0, lblInfoMsg);
                return;
            }
        }
        catch (RfcAbapException ex)  //此Exception专门用于获取用户自定义的异常信息!!!!
        {
            twMsgbox.AjaxAlert(companyBapi.Metadata.GetAbapException(ex.Key).Documentation);
        }
        catch (RfcAbapRuntimeException ex)   //此Exception专门用于获取RFC执行过程中的运行时异常!!!!
        {
            twMsgbox.AjaxAlert(companyBapi.Metadata.GetAbapException(ex.Key).Documentation);
        }
        catch (RfcBaseException ex)  //此Exception是总Exception类,可以获取所有的异常,如果有多个Catch,则不可以放第一位!!!!
        {
            twMsgbox.AjaxAlert("工单号码不存在或者SAP数据库连接出错!信息如下:" + ex.Message);
        }

 方法二

        DataTable data = new DataTable();
        data = adal.GetList2(model.Q02, model.Q05).Tables[0];

        IDestinationConfiguration ID = new RfcConfig();
        RfcDestinationManager.RegisterDestinationConfiguration(ID);
        RfcDestination prd = RfcDestinationManager.GetDestination("PRD_000");//正式区000环境
        RfcDestinationManager.UnregisterDestinationConfiguration(ID);
        RfcRepository repo = prd.Repository;
        IRfcFunction companyBapi = repo.CreateFunction("ZRFC_QUO_GET_OLD_INFO");   //调用函数名

        try
        {
            IRfcTable tableSap = companyBapi.GetTable("IT_ZSMMINFO");
            for (int i = 0; i < data.Rows.Count; i++)
            {
               
                tableSap.CurrentRow.SetValue("LIFNR", data.Rows[i]["QJ13"].ToString());
                tableSap.CurrentRow.SetValue("MATNR", data.Rows[i]["QJ06"].ToString());
                

            }
            companyBapi.Invoke(prd);   //执行函数
            IRfcTable OPTIONS = companyBapi.GetTable("IT_ZSMMINFO");
            DataTable dt = new DataTable();
           
            dt.Columns.Add("PSTYP", Type.GetType("System.String"));
            dt.Columns.Add("LIFNR", Type.GetType("System.String"));

            for (int i = 0; i < OPTIONS.RowCount; i++)
            {
                OPTIONS.CurrentIndex = i;
                DataRow dr = dt.NewRow();
                int a = dt.Rows.Count;
                dt.Rows.Add(dr);
                dt.Rows[a]["PSTYP"] = OPTIONS.GetString("PSTYP");
                dt.Rows[a]["LIFNR"] = OPTIONS.GetString("LIFNR");
            }

            sapdt = dt;
        }
        catch (RfcAbapException ex)  //此Exception专门用于获取用户自定义的异常信息!!!!
        {
            twMsgbox.AjaxAlert(companyBapi.Metadata.GetAbapException(ex.Key).Documentation);
        }
        catch (RfcAbapRuntimeException ex)   //此Exception专门用于获取RFC执行过程中的运行时异常!!!!
        {
            twMsgbox.AjaxAlert(companyBapi.Metadata.GetAbapException(ex.Key).Documentation);
        }
        catch (RfcBaseException ex)  //此Exception是总Exception类,可以获取所有的异常,如果有多个Catch,则不可以放第一位!!!!
        {
            twMsgbox.AjaxAlert("SAP出错!信息如下:" + ex.Message);
        }
你的一分支持,是我坚持创作的十分动力。 如果文章的内容对你有帮助的话,请用微信扫描下方二维码,支持一下。谢谢! wechat1
原文地址:https://www.cnblogs.com/shuilong/p/11307050.html