关键词:在代码中设置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,如下图:
INPUT的具体结构如下图:
2)输入/输出参数 Tables:有很多,下面每一行都是一个(后面代码中以 JHNUM和JHTYP为例),如下图:
其中 JHNUM的结构如下图:
其中 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.代码
<span style="font-family:Microsoft YaHei;font-size:14px;">
//C#代码创建Sap连接对象的类SapConnection
public class SapConnection : IDestinationConfiguration
{
public RfcConfigParameters GetParameters(string destinationName)
{
RfcConfigParameters conf = new RfcConfigParameters();
if (destinationName == "NSP")//给连接SAP的对象命个名
{
conf.Add(RfcConfigParameters.AppServerHost, "sap服务器IP地址");
conf.Add(RfcConfigParameters.SystemNumber, "sap实例号 如00");
conf.Add(RfcConfigParameters.SystemID, "");
conf.Add(RfcConfigParameters.User, "登录sap账号");
conf.Add(RfcConfigParameters.Password, "密码");
conf.Add(RfcConfigParameters.Client, "sap的Client号");
conf.Add(RfcConfigParameters.Language, "ZH或者EN");
}
return conf;
}
public bool ChangeEventsSupported()
{
return true;
}
public event RfcDestinationManager.ConfigurationChangeHandler ConfigurationChanged;
}
//自定义的调用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;
for (int j = 0; j < dt.Rows.Count; j++)
{
DataRow dr = dt2.NewRow();
foreach (string key in rfcHeaderKeys)
{
dr[key] = headTable.GetString(key);
}
dt2.Rows.Add(dr);
}
}
return dt2;
}
}<span style="white-space: normal;">
</span></span>
.NET用NCO连接SAP 从RFC查询数据 代码配置连接SAP的信息
原文地址:http://blog.csdn.net/xuanwuziyou/article/details/44194779