RFC调用SAP接口程序之直接读取SAP表案例4

这个需要有SAP相关基础知识。最起码SAP表结构要熟悉。

    //根据品号读取计量单位
            IDestinationConfiguration ID = new RfcConfig();
            RfcDestinationManager.RegisterDestinationConfiguration(ID);
            RfcDestination prd = RfcDestinationManager.GetDestination("PRD_000");
            RfcDestinationManager.UnregisterDestinationConfiguration(ID);
            RfcRepository repo = prd.Repository;
            IRfcFunction companyBapi = repo.CreateFunction("RFC_READ_TABLE");   //调用函数名
            IRfcFunction companyBapi1 = repo.CreateFunction("RFC_READ_TABLE");   //调用函数名

            try
            {
                companyBapi.SetValue("QUERY_TABLE", "MARM");                        //设置Import的参数

                IRfcTable OPTIONS = companyBapi.GetTable("OPTIONS");
                OPTIONS.Insert();
                OPTIONS.CurrentRow.SetValue("TEXT", "MATNR = '" + txt01.Text.Trim().ToUpper() + "'");
                companyBapi.SetValue("OPTIONS", OPTIONS);

                IRfcTable FIELDS = companyBapi.GetTable("FIELDS");
                FIELDS.Insert();
                FIELDS.CurrentRow.SetValue("FIELDNAME", "MEINH");
                FIELDS.CurrentRow.SetValue("LENGTH", "3");
                FIELDS.CurrentRow.SetValue("TYPE", "C");
                FIELDS.CurrentRow.SetValue("FIELDTEXT", "MEINH");

                companyBapi.SetValue("FIELDS", FIELDS);
                companyBapi.Invoke(prd);   //执行函数
                IRfcTable RETURNTable = companyBapi.GetTable("DATA");

                for (int i = 0; i < RETURNTable.RowCount; i++)
                {
                    RETURNTable.CurrentIndex = i;
                    ddlcs.ddlInsertValue(ddl01, RETURNTable.GetString("WA").Trim());
                }

                ddlcs.ddlInsertValue(ddl01, "");

                companyBapi1.SetValue("QUERY_TABLE", "MBEW");                        //设置Import的参数

                IRfcTable OPTIONS1 = companyBapi1.GetTable("OPTIONS");
                OPTIONS1.Insert();
                OPTIONS1.CurrentRow.SetValue("TEXT", "MATNR = '" + txt01.Text.Trim().ToUpper() + "' AND BWKEY = '1601'");
                companyBapi1.SetValue("OPTIONS", OPTIONS1);

                IRfcTable FIELDS1 = companyBapi1.GetTable("FIELDS");
                FIELDS1.Insert();
                FIELDS1.CurrentRow.SetValue("FIELDNAME", "BKLAS");
                FIELDS1.CurrentRow.SetValue("LENGTH", "4");
                FIELDS1.CurrentRow.SetValue("TYPE", "C");
                FIELDS1.CurrentRow.SetValue("FIELDTEXT", "BKLAS");

                companyBapi1.SetValue("FIELDS", FIELDS1);
                companyBapi1.Invoke(prd);   //执行函数
                IRfcTable RETURNTable1 = companyBapi1.GetTable("DATA");

                if (RETURNTable1.RowCount == 1)
                {
                    RETURNTable1.CurrentIndex = 0;
                    hd01.Value = RETURNTable1.GetString("WA").Trim();//评价类别
                }
                else
                {
                    hd01.Value = null;
                }
            }
            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);
            }

 

posted @ 2020-11-07 14:14  水龙  阅读(625)  评论(0编辑  收藏  举报