.NET用NCO联接SAP 从RFC查询数据 代码配置连接SAP的信息

.NET用NCO连接SAP 从RFC查询数据 代码配置连接SAP的信息

关键词:在代码中设置SAP连接信息,从RFC查询数据解析成DataTable

1.环境:

a. win7+64位操作系统    b. VS2012  

c. nco3.0,是SAP针对.Net开发的专用组件(64bit 下载网址:http://www.dllbang.com/dll/sapnco_dll )
d. (可能用到)Microsoft Visual C++ 2005 Service Pack 1,用于运nco3.0的dll文件
    下载网址:http://www.microsoft.com/en-us/download/details.aspx?id=14431 
e. 安装NCO3.0后在C:\Program Files\SAP\SAP_DotNetConnector3_x86目录下面会有sapnco_utils.dll sapnco.dll rscp4n.dll libicudecnumber.dll四个DLL文件,在项目里面添加引用前两个 DLL文件即可(可在项目中建立一个文件夹,放置这两个dll,再引用文件夹里的这两个dll),还要将项目的.NET目标平台改为4.0。

2.目的:连接SAP的一个查询数据的RFC,调用并解析返回数据成DataTable

3.RFC结构:ZCP3_MMIF002ZBZZ125 的结构:

  1)输入参数 Import : INPUT,如下图:

.NET用NCO联接SAP 从RFC查询数据 代码配置连接SAP的信息

  INPUT的具体结构如下图:
.NET用NCO联接SAP 从RFC查询数据 代码配置连接SAP的信息

2)输入/输出参数 Tables:有很多,下面每一行都是一个(后面代码中以 JHNUM和JHTYP为例),如下图:

.NET用NCO联接SAP 从RFC查询数据 代码配置连接SAP的信息

其中 JHNUM的结构如下图:.NET用NCO联接SAP 从RFC查询数据 代码配置连接SAP的信息

其中 JHTYP的结构和上面JHNUM的结构一样。

3)输出参数-查询结果,是 输入/输出参数Tables中的 HEADER,如下图:
.NET用NCO联接SAP 从RFC查询数据 代码配置连接SAP的信息

HEADER的结构如下:(后面C#代码中就查询HEADER中的这些字段)
.NET用NCO联接SAP 从RFC查询数据 代码配置连接SAP的信息

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>

1楼zhaojiansay2小时前
双双给力哇