访问SAP的RFC

.NET

环境
Xp(sp3) vs2010, win2003 EN 32bit(sp2)
winform,webform

引用sapnco.dll,sapnco_utils.dll(自动引用)
配置文件需要改成混合模式

访问SAP的RFC
<?xml version="1.0"?>

<configuration>

  <startup>

    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />

  </startup>

  <startup useLegacyV2RuntimeActivationPolicy="true">

    <supportedRuntime version="v4.0" />

  </startup>

</configuration>
View Code

代码

访问SAP的RFC
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;



using SAP.Middleware.Connector;

using System.IO;



namespace RMBreakSync

{





    //登陆SAP前的准备工作

    public class MyBackendConfig : IDestinationConfiguration

    {

        public RfcConfigParameters GetParameters(String destinationName)

        {

            if ("PRD_xxx".Equals(destinationName))

            {

                RfcConfigParameters parms = new RfcConfigParameters();

                parms.Add(RfcConfigParameters.AppServerHost, "10.**.**.***");   //SAP主机IP

                parms.Add(RfcConfigParameters.SAPRouter, "/H/2*8.??.**.***/H/");   //SAP主机IP

                parms.Add(RfcConfigParameters.SystemNumber, "00");  //SAP实例

                parms.Add(RfcConfigParameters.User, "xxx");  //用户名

                parms.Add(RfcConfigParameters.Password, "xxx");  //密码

                parms.Add(RfcConfigParameters.Client, "100");  // Client

                parms.Add(RfcConfigParameters.Language, "ZH");  //登陆语言

                return parms;

            }

            else return null;

        }

        public bool ChangeEventsSupported()

        {

            return false;

        }

        public event RfcDestinationManager.ConfigurationChangeHandler ConfigurationChanged;





        public  static bool IsAlive = false;



        private static RfcDestination _RfcDest = null;

        /// <summary>

        /// 获取验证票

        /// </summary>

        /// <returns></returns>

        public static  RfcDestination GetRfcDest()

        {

            if (_RfcDest == null || IsAlive==false)

            {

                lock (typeof(string))

                {

                    if (_RfcDest == null || IsAlive == false)

                    {

                        IDestinationConfiguration ID = new MyBackendConfig();

                        RfcDestinationManager.RegisterDestinationConfiguration(ID);

                        RfcDestination prd = RfcDestinationManager.GetDestination("PRD_xxx");

                        RfcDestinationManager.UnregisterDestinationConfiguration(ID);

                        _RfcDest = prd;

                        IsAlive = true;

                    }

                }

            }





            return _RfcDest;

        }



        





    }



    public class SAPWrap

    {

        /// <summary>

        /// 更新服务器状态

        /// </summary>

        /// <param name="planId"></param>

        /// <param name="batno"></param>

        /// <param name="FGPartno"></param>

        public static void UpdateServerStatus(List<p_prodplanImport> list)

        {

            var prd = MyBackendConfig.GetRfcDest();

            RfcRepository repo = prd.Repository;

            IRfcFunction companyBapi = repo.CreateFunction("ZTPP_TO_MFG_FLAG");   //调用函数名

            IRfcTable rfcTable = companyBapi.GetTable("ITAB");

           // companyBapi.SetValue("EX_WERKS", "3003");   //设置Import的参数 ,即:输入参数



           //多行

            foreach (var plan in list)

            {

                rfcTable.Insert();

                rfcTable.CurrentRow.SetValue("PLANID", plan.PlanID);

                rfcTable.CurrentRow.SetValue("FGPARTNO", plan.P8code);

                rfcTable.CurrentRow.SetValue("BATNO", plan.batno.ToString());

                rfcTable.CurrentRow.SetValue("FLAG", "X");

            }



            



            companyBapi.Invoke(prd);   //执行函数      



      

           

        }



        public static List<RMBreakImport> DownItems(p_prodplanImport plan)

        {

            var prd = MyBackendConfig.GetRfcDest();

            RfcRepository repo = prd.Repository;

            IRfcFunction companyBapi = repo.CreateFunction("ZTPP_TO_MFG_ITEM");   //调用函数名



            IRfcTable rfcTable = companyBapi.GetTable("ITAB");



            //单行

            rfcTable.Insert();

            rfcTable.CurrentRow.SetValue("PLANID", plan.PlanID);

            rfcTable.CurrentRow.SetValue("FGPARTNO", plan.P8code);

            rfcTable.CurrentRow.SetValue("BATNO", plan.batno.ToString().Trim());

         

    

            companyBapi.Invoke(prd);   //执行函数        





            IRfcTable table = companyBapi.GetTable("ZITEM");  //获取相应的品号内表



            var list = new List<RMBreakImport>();

            for (int i = 0; i < table.RowCount; i++)

            {

                table.CurrentIndex = i;  //当前内表的索引行



                var item = new RMBreakImport();

                item.PlanId = table.GetString("PLANID");

                item.PlanId = item.PlanId.TrimStart("0".ToCharArray());



                item.FGpartno = table.GetString("FGPARTNO"); //前导零

                item.FGpartno = item.FGpartno.TrimStart("0".ToCharArray());





                item.p_yw = table.GetString("P_YW");

                item.p_FGName = table.GetString("P_FGNAME");

                item.batno = decimal.Parse(table.GetString("BATNO"));

                item.Poutput = table.GetDecimal("POUTPUT");

                item.productdate =DateTime.Parse( table.GetString("ProductDate")).ToString("yyyy-MM-dd");



                item.partno = table.GetString("PartNO");

                item.partno = item.partno.TrimStart("0".ToCharArray());



                item.p_partnoName = table.GetString("P_PartNoName");

                item.partnoqty = table.GetDecimal("PartNoQty");

                item.partnototalqty = table.GetDecimal("PartNoTotalQty");

                item.UM = table.GetString("UM");

                item.p_brand = table.GetString("P_BRAND");

                item.p_supplier = table.GetString("NORMT");

                item.BreakDate =DateTime.Parse(table.GetString("BreakDate")).ToString("yyyy-MM-dd");

                item.Breaker = table.GetString("BREAKER");

                //设置默认值

                item.TicketStatus = "已审核";

                item.IsOut = "";

                item.p_type = string.Empty;



                list.Add(item);



            }







            return list;

        }



        /// <summary>

        /// 下载计划列表

        /// </summary>

        /// <returns></returns>

        public static List<p_prodplanImport> DownPlan()

        {

            var prd = MyBackendConfig.GetRfcDest();

            RfcRepository repo = prd.Repository;

            IRfcFunction companyBapi = repo.CreateFunction("ZTPP_TO_MFG");   //调用函数名

            companyBapi.SetValue("EX_WERKS", "3003");   //设置Import的参数 ,即:输入参数          

            companyBapi.Invoke(prd);   //执行函数        

            string MAKTX = companyBapi.GetValue("EX_WERKS").ToString();  //获取字段

   

           

            IRfcTable table = companyBapi.GetTable("ITAB");  //获取相应的品号内表

   

            var list=new List<p_prodplanImport>();

            



            for (int i = 0; i < table.RowCount; i++)

            {

                table.CurrentIndex = i;  //当前内表的索引行



                var plan=new p_prodplanImport();

                plan.PlanID = table.GetString("PLANID");

                plan.PlanID = plan.PlanID.TrimStart("0".ToArray());//前导零



                plan.P8code = table.GetString("FGPartNO");

                plan.P8code = plan.P8code.TrimStart("0".ToArray());

                plan.Pname = table.GetString("pname"); //计划名称 

                plan.PlanType = table.GetString("PlanType");

                plan.Ppname = table.GetString("P_FGName");

                plan.Psetting = table.GetString("PSetting");

                if (plan.Psetting.Length > 50) plan.Psetting = plan.Psetting.Substring(0, 50);

                plan.batno =decimal.Parse( table.GetString("batno"));

                plan.P_yw = table.GetString("P_YW");

                plan.Poutput = table.GetDecimal("Poutput");

                plan.Pdate =DateTime.Parse( table.GetString("ProductDate")).ToString("yyyy-MM-dd");



                //设置默认列



                plan.Pweek1 = plan.Pdate;

                plan.Pplanner = "";

                plan.Pmanger = "";

                plan.Pcheckreust = "通过";

                plan.Pcheckdate = plan.Pdate;

                plan.Ptype = "新制";

                plan.Pday1 = plan.Poutput;



    

                list.Add(plan);



            }





           

            return list;



        }





    }









}
View Code

 Java

使用JCO,jco里面有4个版本,需要选择正确的版本,测试Jco是否正确可以使用 java -jar d:\jco\sapjco3.jar ,如果没有报错表示正常了

访问SAP的RFC
 public static List<String> callRfcExample() {

        // 获取RFC 对象

        JCoFunction function = RfcManager.getFunction("xxxx_V1");

        // 设置import 参数

        JCoParameterList importParam = function.getImportParameterList();

        importParam.setValue("I_WERKS", "www1");

        

        

        JCoTable tablename = function.getTableParameterList().getTable("R_GSTRP");

        tablename.deleteRow();

        tablename.deleteAllRows();

        tablename.clear();

        tablename.firstRow();

        

        tablename.appendRow();

        tablename.firstRow();



        tablename.setValue("SIGN", "I");

        tablename.setValue("OPTION", "BT");

        tablename.setValue("LOW", "2013-06-25");

        tablename.setValue("HIGH", "2013-06-25");

        // 执行RFC

        RfcManager.execute(function);



        // 获取RFC返回的字段值

//        JCoParameterList exportParam = function.getExportParameterList();

//        String exParamA = exportParam.getString("field_A");

//        String exParamB = exportParam.getString("field_B");

        // 遍历RFC返回的表对象

        List<String> list=new ArrayList<String>();

        

        JCoTable tb = function.getTableParameterList().getTable("ZAFKO");

        for (int i = 0; i < tb.getNumRows(); i++) {

            tb.setRow(i);

            list.add(tb.getString("ORDER_NUMBER"));

            System.out.println(tb.getString("ORDER_NUMBER"));

            //System.out.println(tb.getString("field02"));

        }

        return list;

    }
View Code

详细请参考这里:http://www.cnblogs.com/geun/archive/2012/11/12/2765793.html#2714359

另外win2003,32或64 bit的需要安装对应的
Microsoft Visual C++ 2005 Service Pack 1 Redistributable Package (KB973544)
话说win2003,32bit上安装了七把次都没成功:(

//============

完成代码参考网盘内容

你可能感兴趣的:(SAP)