因工作需要在MES呼叫SAP RFC,原本之前用SAP Connector .NET 2.0,而且只能支持vs2003,自己创建一个Class,添加SAPProxy编译dll后才能使用。现在有SAP Connector .NET 3.0就可直接支持vs2005以上,方便多了。SAP Connector .NET 3.0下载在csdn资源里就有。下载安装后,引用两个dll(sapnco.dll,sapnco_utils.dll),现在将参考http://www.cnblogs.com/mengxin523/写的一个范例贴出供大家参考。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SAP.Middleware.Connector;
namespace SAPStudio
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string MATNR = string.Empty;
string SERNR = string.Empty;
private void Form1_Load(object sender, EventArgs e)
{
IDestinationConfiguration ID = new SAPConfig();
RfcDestinationManager.RegisterDestinationConfiguration(ID);
RfcDestination dest = RfcDestinationManager.GetDestination("SAPMES");
RfcDestinationManager.UnregisterDestinationConfiguration(ID);
Zmed007(dest);
Zemd002(dest);
}
public void Zemd002(RfcDestination dest)
{
RfcRepository repository = dest.Repository;
IRfcFunction rfc = repository.CreateFunction("ZMED002");
string begin = "2011-01-01";
string end = "2011-09-20";
rfc.SetValue("date_from", begin);
rfc.SetValue("date_to", end);
rfc.Invoke(dest);
IRfcTable table = rfc.GetTable("ZMED002"); //获取相应的业务内表
DataTable dt = new DataTable(); //新建表格
dt.Columns.Add("KUNNR"); //表格添加一列
dt.Columns.Add("NAME1");
for (int i = 0; i < table.RowCount; i++)
{
table.CurrentIndex = i; //当前内表的索引行
DataRow dr = dt.NewRow();
dr[0] = table.GetString("KUNNR");
dr[1] = table.GetString("NAME1");
dt.Rows.Add(dr); //填充该表格的值
}
dataGridView1.DataSource = dt;
dest = null;
repository = null;
}
public void Zmed007(RfcDestination dest)
{
RfcRepository repository = dest.Repository;
IRfcFunction rfc = repository.CreateFunction("ZMED007"); //调用函数名
MATNR = "100028570G";
SERNR = "0002D10AD026";
rfc.SetValue("MATNR", MATNR); //设置Import的参数
rfc.SetValue("SERNR", SERNR);
rfc.Invoke(dest); //执行函数
string ERMSG = rfc.GetValue("ERMSG").ToString();
this.label1.Text = ERMSG;
dest = null;
repository = null;
}
}
public class SAPConfig : IDestinationConfiguration
{
public RfcConfigParameters GetParameters(string destinationName)
{
if (destinationName.Equals("SAPMES"))
{
RfcConfigParameters rfcParams = new RfcConfigParameters();
rfcParams.Add(RfcConfigParameters.AppServerHost, "192.168.0.19"); //SAP主机IP
rfcParams.Add(RfcConfigParameters.SystemNumber, "00"); //SAP实例
rfcParams.Add(RfcConfigParameters.User, "mes"); //用户名
rfcParams.Add(RfcConfigParameters.Password, "123456"); //密码
rfcParams.Add(RfcConfigParameters.Client, "100"); // Client
rfcParams.Add(RfcConfigParameters.Language, "zf"); //登陆语言
rfcParams.Add(RfcConfigParameters.PoolSize, "5");
rfcParams.Add(RfcConfigParameters.MaxPoolSize, "10");
rfcParams.Add(RfcConfigParameters.IdleTimeout, "60");
return rfcParams;
}
else
{
return null;
}
}
public bool ChangeEventsSupported()
{
return false;
}
public event RfcDestinationManager.ConfigurationChangeHandler ConfigurationChanged;
}
}