1. 程式人生 > 實用技巧 >RFC呼叫SAP介面程式之直接讀取SAP表案例4

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