登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

忆风的博客

http://www.cnblogs.com/dhjdhja

 
 
 

日志

 
 
 
 

SAP-ABAP函数与RFC  

2011-06-07 18:30:20|  分类: SAP |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

事务代码se37

新建函数时要先建函数组,打开菜单Springen->FGruppenverwaltung->Gruppe anlegen新建函数组。

新建函数,输入名称F5新建。选择函数组,输入描述。

Attributes 基本属性(如果要用于外部调用 Processing Type选Remote-Enabled Module)
Import 输入参数(外部调用 Pass Value打钩)
Export 输入值(外部调用 Pass Value打钩)
Tables 输入输出的表

例:
Attributes: Processing Type选Remote-Enabled Module
Import: I_MATNR
Tables: 输出表E_MARA

Source Code
FUNCTION ZDTFTEST1.
SELECT *
  INTO CORRESPONDING FIELDS OF TABLE  E_MARA
  FROM MARA
  WHERE MATNR = I_MATNR.
ENDFUNCTION.

C# RFC 调用一
引用Interop.SAPFunctionsOCX.dll、Interop.SAPTableFactoryCtrl.dll、Interop.SAPLogonCtrl.dll
前两个有的改名为wdtfuncs.ocx、WDTAOCX.OCX

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SAPFunctionsOCX;
using SAPLogonCtrl;
using SAPTableFactoryCtrl;
namespace WindowsFormsApplication2
{
    public partial class Form2 : Form
    {
        DataTable dt;
        public Form2()
        {
            InitializeComponent();
            dt = new DataTable();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            System.Threading.Thread s = new System.Threading.Thread(new System.Threading.ThreadStart(testTable2)); //Create a new thread and set the method test() run in this thread
            s.SetApartmentState(System.Threading.ApartmentState.STA);                                        //Set the run mode 'STA'
            s.Start();                                                                                       //Start the thread
            s.Join();                                                                                        //Wait until thread run OK.
            dataGridView1.DataSource = dt;

        }

        private void testTable2()
        {
            SAPLogonCtrl.SAPLogonControlClass login = new SAPLogonCtrl.SAPLogonControlClass();
            login.ApplicationServer = "192.168.1.108";
            login.Client = "811";
            login.Language = "ZH";
            login.User = "LIANXI10";
            login.Password = "sap123";
            login.SystemNumber = 00;
            SAPLogonCtrl.Connection conn = (SAPLogonCtrl.Connection)login.NewConnection();
            if (conn.Logon(0, true))
            {
                SAPFunctionsOCX.SAPFunctionsClass func = new SAPFunctionsOCX.SAPFunctionsClass();
                func.Connection = conn;
                //传入参数
                SAPFunctionsOCX.IFunction ifunc = (SAPFunctionsOCX.IFunction)func.Add("ZDTFTEST1");
                SAPFunctionsOCX.IParameter gclient = (SAPFunctionsOCX.IParameter)ifunc.get_Exports("I_MATNR");
                gclient.Value = "100-100";
                //调用
                ifunc.Call();

                ////接收返回值
                //SAPFunctionsOCX.IParameter a = (SAPFunctionsOCX.IParameter)ifunc.get_Imports("E_MAKTX");
                //MessageBox.Show(a.Value.ToString());
                //接收返回表
                SAPTableFactoryCtrl.Tables tables = (SAPTableFactoryCtrl.Tables)ifunc.Tables;
                SAPTableFactoryCtrl.Table ENQ = (SAPTableFactoryCtrl.Table)tables.get_Item("E_MARA");
                int n = ENQ.RowCount;
                dt = new DataTable();
                dt.Columns.Add("MANDT");
                dt.Columns.Add("MATNR");
                for (int i = 1; i <= n && i < 10; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr["MANDT"] = ENQ.get_Cell(i, "MANDT").ToString();
                    dr["MATNR"] = ENQ.get_Cell(i, "MATNR").ToString();
                    dt.Rows.Add(dr);
                }
            }
        }
    }
}

 

 

C# RFC 调用二
引用sapnco.dll、sapnco_utils.dll
using SAP.Middleware.Connector;

    public class MyBackendConfig : IDestinationConfiguration
    {
        public RfcConfigParameters GetParameters(String destinationName)
        {
            if ("PRD_000".Equals(destinationName))
            {
                RfcConfigParameters parms = new RfcConfigParameters();
                parms.Add(RfcConfigParameters.AppServerHost, "192.168.1.108");
                parms.Add(RfcConfigParameters.SystemNumber, "00");
                parms.Add(RfcConfigParameters.User, "lianxi10");
                parms.Add(RfcConfigParameters.Password, "sap123");
                parms.Add(RfcConfigParameters.Client, "811");
                parms.Add(RfcConfigParameters.Language, "ZH");
                parms.Add(RfcConfigParameters.PoolSize, "5");
                parms.Add(RfcConfigParameters.MaxPoolSize, "10");
                parms.Add(RfcConfigParameters.IdleTimeout, "600");
                return parms;
            }
            else return null;
        }
        // The following two are not used in this example:
        public bool ChangeEventsSupported()
        {
            return false;
        }
        public event RfcDestinationManager.ConfigurationChangeHandler
        ConfigurationChanged;
    }

 

 class Class1
    {
        public static void Maint()
        {
            RfcDestinationManager.RegisterDestinationConfiguration(new MyBackendConfig());
            RfcTransaction trans = new RfcTransaction();
            RfcTID tid = trans.Tid;
            String[] payload = { "a" }; // In this example we just send a bunch of strings into

            RfcDestination prd = RfcDestinationManager.GetDestination("PRD_000");
            try
            {
                RfcRepository repo = prd.Repository;
                IRfcFunction stfcWrite = repo.CreateFunction("ZDTRFCTABLE2");
                stfcWrite.SetValue("I_T", 1);
                IRfcTable data = stfcWrite.GetTable("I_EMP");
                data.Append(payload.Length);

                data[0].SetValue("MANDT", "811");
                data[0].SetValue("ZDTNO", "a1");
                data[0].SetValue("ZDTNAME","ccc");
                data.Append();


                data[1].SetValue("MANDT", "811");
                data[1].SetValue("ZDTNO", "a2");
                data[1].SetValue("ZDTNAME", "ddd");


                trans.AddFunction(stfcWrite);
                trans.Commit(prd);


                stfcWrite.Invoke(prd);
                string ss = stfcWrite.GetInt("E_MSG").ToString();
                IRfcTable data1 = stfcWrite.GetTable("E_EMP");
                for (int i = 0; i < data1.RowCount; i++)
                {
                }

            }
            catch (Exception e)
            {
                return;
            }
         
            prd.ConfirmTransactionID(tid); // This deletes the tid from ARFCRSTATE on
            // backend side.
        }
    }

  评论这张
 
阅读(3894)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018