新建函数时要先建函数组,打开菜单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.
}
}
评论