1、安裝sap .net connector 3.0 程式,附件中提供
注意根據自己的環境選擇:
Compiled with .NET Framework 2.0:
sapnco30P_8-20007347.zip:SAP Connector for Microsoft .NET 3.0.8.0 for Windows 32bit (x86)
sapnco30P_8-20007348.zip:SAP Connector for Microsoft .NET 3.0.8.0 for Windows 64bit (x64)
Compiled with .NET Framework 4.0
sapnco30dotnet40P_8-20007347.zip:SAP Connector for Microsoft .NET 3.0.8.0 for Windows 32bit (x86)
sapnco30dotnet40P_8-20007348.zip: SAP Connector for Microsoft .NET 3.0.8.0 for Windows 64bit (x64)
2、在程式中引用 sapnco.dll ,sapnco_utils.dll
例如:C:\Program Files (x86)\SAP\SAP_DotNetConnector3_x64/下 (根據自己安裝情況)
3、部分代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SAP.Middleware.Connector;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
string MATNR = string.Empty;
public Form1()
{
InitializeComponent();
}
public void Execute()
{
IDestinationConfiguration ID = new MyBackendConfig();
RfcDestinationManager.RegisterDestinationConfiguration(ID);
//登入
RfcDestination prd = RfcDestinationManager.GetDestination("my_asp"); // 這個名稱為自己定義,注意和 MyBackendConfig 中一樣就行
Execute(prd);
//退出登入
RfcDestinationManager.UnregisterDestinationConfiguration(ID);
}
public void Execute(RfcDestination prd)
{
RfcRepository repo = prd.Repository;
IRfcFunction companyBapi = repo.CreateFunction("sap_Function_name"); //調用函數名
companyBapi.SetValue("param1", "value1"); //設定Import的參數
companyBapi.SetValue("param2", "value2"); //設定Import的參數
companyBapi.Invoke(prd); //執行函數
IRfcTable table = companyBapi.GetTable("out_table");//這個根據自己的參數設定
//聲明欄位,此欄位放入資料庫可以實現欄位的動態設定
string[] columns = new string[]{
"clumn1",
"clumn2",
"clumn3",
"clumn4",
"clumn5"
};
DataTable dt = new DataTable(); //建立表格
foreach (string clmn in columns)
{
dt.Columns.Add(clmn);
}
for (int i = 0; i < table.RowCount; i++)
{
DataRow dr = dt.NewRow();
foreach (string clmn in columns)
{
dr[clmn] = table.GetString(clmn);
}
dt.Rows.Add(dr); //填充該表格的值
}
this.dataGridView1.DataSource = dt;
prd = null;
repo = null;
}
//登陸SAP前的準備工作
public class MyBackendConfig : IDestinationConfiguration
{
public RfcConfigParameters GetParameters(String destinationName)
{
if ("my_sap".Equals(destinationName))
{
RfcConfigParameters parms = new RfcConfigParameters();
parms.Add(RfcConfigParameters.AppServerHost, "10.10.10.2*"); //SAP主機IP
parms.Add(RfcConfigParameters.SystemNumber, "2*"); //SAP執行個體
parms.Add(RfcConfigParameters.User, "username"); //使用者名稱
parms.Add(RfcConfigParameters.Password, "password"); //密碼
parms.Add(RfcConfigParameters.Client, "800"); // Client
parms.Add(RfcConfigParameters.Language, "ZH"); //登陸語言
parms.Add(RfcConfigParameters.PoolSize, "5");
parms.Add(RfcConfigParameters.IdleTimeout, "60");
return parms;
}
else
return null;
}
public bool ChangeEventsSupported()
{
return false;
}
public event RfcDestinationManager.ConfigurationChangeHandler ConfigurationChanged;
}
private void button1_Click(object sender, EventArgs e)
{
Execute();
}
}
}