关键词:在代码中设置SAP连接信息,从RFC查询数据解析成DataTable
1.环境:
a. win7+64位操作系统 b. VS2012
c. nco3.0,是SAP针对.Net开发的专用组件(64bit 下载网址:http://www.dllbang.com/dll/sapnco_dll )2.目的:连接SAP的一个查询数据的RFC,调用并解析返回数据成DataTable
3.RFC结构:ZCP3_MMIF002ZBZZ125 的结构:
1)输入参数 Import : INPUT,如下图:
2)输入/输出参数 Tables:有很多,下面每一行都是一个(后面代码中以 JHNUM和JHTYP为例),如下图:
其中 JHTYP的结构和上面JHNUM的结构一样。
3)输出参数-查询结果,是 输入/输出参数Tables中的 HEADER,如下图:
HEADER的结构如下:(后面C#代码中就查询HEADER中的这些字段)
4.测试用查询条件:
INPUT里的BUKRS:K999
INPUT里的WERKS:X666
Tables里的JHNUM: SIGN:I OPTION:EQ LOW:00000001 HIGH: (空字符串"")
Tables里的JHTYP: SIGN:I OPTION :BT LOW:1 HIGH:2
5.代码
//C#代码创建Sap连接对象的类SapConnection public class SapConnection : IDestinationConfiguration { public RfcConfigParameters GetParameters(string destinationName, SAPClient sapInfo) { RfcConfigParameters conf = new RfcConfigParameters(); if (destinationName == "NSP")//给连接SAP的对象命个名 { //先添加必填参数 conf.Add(RfcConfigParameters.SystemNumber, sapInfo.SystemNumber); conf.Add(RfcConfigParameters.SystemID, ""); conf.Add(RfcConfigParameters.User, sapInfo.User); conf.Add(RfcConfigParameters.Password, sapInfo.Password); conf.Add(RfcConfigParameters.Client, sapInfo.Client); conf.Add(RfcConfigParameters.Language, sapInfo.Language); //添加可选参数(普通登陆参数) if (!string.IsNullOrEmpty(sapInfo.ApplicationServer)) conf.Add(RfcConfigParameters.AppServerHost, sapInfo.ApplicationServer); if (!string.IsNullOrEmpty(sapInfo.AppServerService)) conf.Add(RfcConfigParameters.AppServerService, sapInfo.AppServerService); //添加可选参数(服务器组参数) if (!string.IsNullOrEmpty(sapInfo.MessageServerHost)) conf.Add(RfcConfigParameters.MessageServerHost, sapInfo.MessageServerHost); if (!string.IsNullOrEmpty(sapInfo.MessageServerService)) conf.Add(RfcConfigParameters.MessageServerService, sapInfo.MessageServerService); if (!string.IsNullOrEmpty(sapInfo.LogonGroup)) conf.Add(RfcConfigParameters.LogonGroup, sapInfo.LogonGroup); if (!string.IsNullOrEmpty(sapInfo.SystemID)) conf.Add(RfcConfigParameters.SystemID, sapInfo.SystemID); } return conf; } public bool ChangeEventsSupported() { return true; } public event RfcDestinationManager.ConfigurationChangeHandler ConfigurationChanged; } //SAP连接参数类 public class SAPClient { public SAPClient(); public string ApplicationServer { get; set; } //应用服务器IP地址,如 10.88.88.88 public string AppServerService { get; set; } public string Client { get; set; } //sap客户端 如 100 public string Language { get; set; } //语言,如 ZH 或 EN public string LogonGroup { get; set; } //登录组名 public string MessageServerHost { get; set; } //消息服务器IP地址 public string MessageServerService { get; set; } public string Password { get; set; } //登录密码 public string SystemID { get; set; } //系统标识 public string SystemNumber { get; set; } //实例号,如 00 public string User { get; set; } //登录账号 } //自定义的调用RFC的类 public class NcoQueryRFC { //创建Sap连接对象的类SapConnection的实例global_dest,单例模式 private static RfcDestination global_dest; public static RfcDestination SapConnInfo() { if (global_dest == null) { SapConnection con = new SapConnection();//SapConnection类定义见代码末 RfcDestinationManager.RegisterDestinationConfiguration(con); } //这里定义 global_dest = RfcDestinationManager.GetDestination("NSP"); return global_dest; } //查询RFC的方法 public static string GetHeadersFromSap() { RfcDestination dest = NcoQueryRFC.SapConnInfo(); RfcRepository rfcrep = dest.Repository; IRfcFunction myfun = rfcrep.CreateFunction("ZCP3_MMIF002ZBZZ125"); //RFC的名称 //传入结构INPUT IRfcStructure input = myfun.GetStructure("INPUT"); input.SetValue("BUKRS", "K999"); input.SetValue("WERKS", "X666"); myfun.SetValue("INPUT", input); //传入Tables表中的 JHNUM IRfcTable table = myfun.GetTable("JHNUM");//RFC中要传入的JHNUM的表 IRfcStructure import = null; var structMeta = rfcrep.GetStructureMetadata("ZCP3MMIF002ZBZZ125_S05");//是SAP里JHNUM这个表参考的结构 import = structMeta.CreateStructure(); import.SetValue("SIGN", "I"); import.SetValue("OPTION", "EQ"); import.SetValue("LOW", "00000001"); import.SetValue("HIGH", ""); table.Insert(import); //传入Tables表中的 JHTYP IRfcTable table2 = myfun.GetTable("JHTYP"); IRfcStructure import2 = null; var structMeta = rfcrep.GetStructureMetadata("ZCP3MMIF002ZBZZ125_S05"); import2 = structMeta.CreateStructure(); import2.SetValue("SIGN", "I"); import2.SetValue("OPTION", "BT"); import2.SetValue("LOW", "1"); import2.SetValue("HIGH", "2"); table2.Insert(import2); myfun.Invoke(dest);//调用 IRfcTable headTable = myfun.GetTable("HEADER");//RFC返回的HEADER结构集合 DataTable dt2 = new DataTable(); string[] rfcHeaderKeys = { "JHNUM", "BUKRS","BUTXT", "WERKS", "NAME1","JHTYP"};//要返回的DataTable里的列名 foreach (string key in rfcHeaderKeys) { dt2.Columns.Add(key); } //循环把IRfcTable里面的数据放入Table里面 for (int i = 0; i < (headTable.Count); i++) { headTable.CurrentIndex = i; DataRow dr = dt2.NewRow(); foreach (string key in rfcHeaderKeys) { dr[key] = headTable.GetString(key); } dt2.Rows.Add(dr); } return dt2; } }