方式1:使用客户端自带的组件
安装客户端以后,添加引用:SAPFunctionsOCX(.net 的Com列表里一般找不到,需要引用DLL【一般位于以下路径:Program Files\SAP\FrontEnd\SAPgui\wdtfuncs.ocx】),SAPLogonCtrl,SAPTableFactoryCtrl。
public DataTable GetRFCData() { Connection conn = null; SAPFunctionsClass func = null; IFunction ifunc = null; conn = this.getSAPConnection(); //返回SAP登录类 func = this.getSAPFunctionsClass(conn); //返回SAP操作类 ifunc = this.getIFunction("ZHRIF01_FM01", func); //返回SAP接口 //设定SAP RFC函数的参数 IParameter IP_DATA_BEGIN = (IParameter)ifunc.get_Exports("I_DATUM"); // IP_DATA_BEGIN.Value = "201602"; // "20100101";//;strDatB; //"20090101"; //CALL RFC函数 ifunc.Call(); //输出参数 Tables Table_Outs = (Tables)ifunc.Tables; Table Table_mkpf = (Table)Table_Outs.get_Item("T_RESULT"); //获取返回结果集 DataTable dt = GetDataTable(Table_mkpf); //this.label1.Text = dt.Rows.Count.ToString(); return dt; } public Connection getSAPConnection() { //SAP登录类 SAPLogonControlClass logon = new SAPLogonControlClass(); Connection Conn; logon.ApplicationServer = "10.1.8.11"; logon.Client = "300"; logon.Language = "ZH"; //EN OR ZH .... //logon.Language = "zh"; //EN OR ZH .... logon.User = "test"; logon.Password = "test1234"; logon.SystemNumber = Convert.ToInt16("00"); Conn = (SAPLogonCtrl.Connection)logon.NewConnection(); //Conn.CodePage = "8300"; //登录SAP if (!Conn.Logon(0, true)) //Login Successful { throw new Exception("登录SAP系统失败,请重新登录!"); } //返回SAP登录类 return Conn; } public SAPFunctionsClass getSAPFunctionsClass(Connection conn) { //SAPFunction Object SAPFunctionsClass func = new SAPFunctionsClass(); //Set Connection func.Connection = conn; return func; } ////// 返回接口 /// /// ///private IFunction getIFunction(string IFunctionName, SAPFunctionsClass func) { IFunction ifunc = (SAPFunctionsOCX.IFunction)func.Add(IFunctionName); return ifunc; } /// /// 将从sap获取的table转换为datatable /// /// ////// private DataTable GetDataTable(Table saptb) { DataTable dt = new DataTable(); try { for (int i = 1; i < saptb.ColumnCount; i++) { //dt.Columns.Add(saptb.get_ColumnName(i), typeof(string)); dt.Columns.Add(saptb.get_ColumnName(i)); } for (int m = 1; m <= saptb.RowCount; m++) { DataRow dr = dt.NewRow(); for (int n= 1; n < saptb.ColumnCount; n++) { string columnName=saptb.get_ColumnName(n); dr[saptb.get_ColumnName(n)] = saptb.get_Cell(m, columnName).ToString(); } dt.Rows.Add(dr); } } catch (Exception ex) { throw ex; } return dt; }
注意:如果出现中文数据全部为#(井)的情况,这说明是CodePage的问题,暂时没找到解决办法。尝试设定SAPConnection的CodePage,结果提示内存不能写入。
方法2:在机器上没有安装客户端的情况下,使用官网提供的Nco3.0.16.0(SAP Connector for Microsoft .Net)。本DLL含有四个版本。
32位和64位,以及基于.Net2.0 或者 4.0编译的组件(本处代码基于:32位 4.0 的组件,开发环境VS2010),安装完毕后需要引用如下DLL文件:sapnco.dll,sapnco_utils.dll(位于安装目录下)
代码如下:
配置文件:
程序代码:
public static DataTable GetSAPTestData() { RfcDestination destination = RfcDestinationManager.GetDestination("DEV"); RfcFunctionMetadata BAPI_COMPANYCODE_GETDETAIL_MD = destination.Repository.GetFunctionMetadata("ZHRIF01_FM01"); IRfcFunction function = null; function = BAPI_COMPANYCODE_GETDETAIL_MD.CreateFunction(); function.SetValue("I_DATUM", "20160203"); //设置参数 //function.SetParameterActive("COMPANYCODE_ADDRESS", false);设置参数 function.Invoke(destination);//提交调用 IRfcTable returnTable = function.GetTable("T_RESULT"); return GetDataTable(returnTable); } ////// 将从sap获取的table转换为datatable /// /// ////// public static DataTable GetDataTable(IRfcTable IrfTable) { DataTable dt = new DataTable(); //遍历元素个数,循环添加列 for(int i=0;i
不使用配置文件直接代码写入参数的方式:
public static DataTable GetSapData() { RfcConfigParameters rfcPar = new RfcConfigParameters(); rfcPar.Add(RfcConfigParameters.Name, "CON"); rfcPar.Add(RfcConfigParameters.AppServerHost, "10.1.8.11"); rfcPar.Add(RfcConfigParameters.Client, "300"); rfcPar.Add(RfcConfigParameters.User, "test"); rfcPar.Add(RfcConfigParameters.Password, "1234"); rfcPar.Add(RfcConfigParameters.SystemNumber, "00"); rfcPar.Add(RfcConfigParameters.Language, "EN"); rfcPar.Add(RfcConfigParameters.Codepage, "8400"); RfcDestination dest = RfcDestinationManager.GetDestination(rfcPar); RfcRepository rfcrep = dest.Repository; IRfcFunction myfun = null; myfun = rfcrep.CreateFunction("ZHRIF01_FM01"); myfun.SetValue("I_DATUM", "20160203");//SAP里面的传入参数 try { myfun.Invoke(dest); } catch (Exception ex) { MessageBox.Show(ex.Message); } IRfcTable IrfTable = myfun.GetTable("T_RESULT"); return GetDataTable(IrfTable); }
posted on 2016-02-24 16:11 阅读( ...) 评论( ...)