之前接手的系統 call SAP RFC 都是用 Connector 2.0 (應該吧!?)
後來有同事分享了 Connector 3.0 的用法
看起來比 2.0 好用很多
舊版本的 RFC 需要引用很多類別
例如: using SAP.Connector; using SAP.Connector.Rfc;
呼叫SAP的欄位需要用 VS2003包個一個dll檔才能引用
所以一旦需要修改 or debug就很麻煩
後來試著改用 Connector 3.0的寫法
真的是好用很多
只需要加入參考兩個 dll ( "sapnco.dll" and "sapnco_utils.dll" )
寫法如下:
1) 開啟Visual Studio 2010 (.net Framework 4.0)
2) 加入參考 (把上述兩個 dll 加進來)
3) using SAP.Middleware.Connector;
4) 開始 Coding... ....
const string ABAP_AS_POOLED = "DV";
RfcDestination SapRfcDestination = RfcDestinationManager.GetDestination(ABAP_AS_POOLED ");
RfcRepository SapRfcRepository = SapRfcDestination.Repository;
IRfcFunction RFC_FUNCTION_NAME= SapRfcRepository.CreateFunction("RFC_FUNCTION_NAME ");
/*========== Read SAP Internal Table ==========*/
IRfcTable table_SAP = RFC_FUNCTION_NAME .GetTable("SAP_INTERNAL_TABLE");
/*========== 輸入Import資料 ==========*/
RFC_FUNCTION_NAME .SetValue("COLUMN1", Value1); //RFC Import Parameter
for (int i=0; i< detail.RowCount; i++)
{
IRfcStructure articol_SAP = table_SAP.Metadata.LineType.CreateStructure();
articol_SAP.SetValue("SAP COLUMN2", Value2);
articol_SAP.SetValue("SAP COLUMN3", Value3);
table_SAP.Append(articol_SAP );
}
RFC_FUNCTION_NAME .Invoke(SapRfcDestination); //RFC execution
/*========== Read SAP Export Data ==========*/
object okCode = RFC_FUNCTION_NAME.GetValue("SAP EXPORT COLUMN");
string str_okCode = Convert.ToString(okCode);
IRfcTable idt= RFC_FUNCTION_NAME[RFC Function Out Table].GetTable();
for(int i=0; idt.RowCount; i++)
{
//set the current row - the row used for Get*/Set* operations
Response.Write(detail.GetValue(i));
}
參考文章: SAP DOTNET CONNECTOR 3.0