好久没写过相关代码,今天又来贡献一篇 C# 使用 SapNwRfc 调用SAP RFC。用VS2022的WINFORM应用程序,使用NuGet中的SapNwRfc类库,call SAP系统中的RFC,传入7个参数,得到RFC返回的2张表的数据。
一、VS2022中新建WINFORM,Nuget中安装SapNwRfc组件。
二、WINFORM中,放两个LISTVIEW显示最后SAP返回的表数据
三、程序点击按钮,代码开一个单独线程处理CALL SAP的RFC,RFC运行后,返回2张表的数据,在LISTVIEW中显示出来。
四、SAP中的RFC定义:
表一结构:
表二结构:
五、全部C#代码,用Listview显示数据也是很快的,只要使用好 listView1.BeginUpdate();和 listView1.EndUpdate();把他们放在循环填数的外面:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using SapNwRfc;
namespace SAPRFC2024
{
public partial class Form1 : Form
{
public Form1()
{
Control.CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
var l1 = new ColumnHeader(); l1.Text = "Z_BOM_LINE"; l1.Width = 200; listView1.Columns.Add(l1);
var l2 = new ColumnHeader(); l2.Text = "TEXT"; l2.Width = 150; listView1.Columns.Add(l2);
var l3 = new ColumnHeader(); l3.Text = "MATNR"; l3.Width = 200; listView1.Columns.Add(l3);
var l4 = new ColumnHeader(); l4.Text = "QUANTITY"; l4.Width = 50; listView1.Columns.Add(l4);
var l5 = new ColumnHeader(); l5.Text = "MEIN"; l5.Width = 50; listView1.Columns.Add(l5);
var l6 = new ColumnHeader(); l6.Text = "WORKSTATION"; l6.Width = 200; listView1.Columns.Add(l6);
var l7 = new ColumnHeader(); l7.Text = "Z_TYPE"; l7.Width = 50; listView1.Columns.Add(l7);
var h1 = new ColumnHeader(); h1.Text = "MNAME"; h1.Width = 200; listView2.Columns.Add(h1);
var h2 = new ColumnHeader(); h2.Text = "ATBEZ"; h2.Width = 150; listView2.Columns.Add(h2);
var h3 = new ColumnHeader(); h3.Text = "MWERT"; h3.Width = 200; listView2.Columns.Add(h3);
var h4 = new ColumnHeader(); h4.Text = "ATWTB"; h4.Width = 50; listView2.Columns.Add(h4);
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
Thread lsThread = new Thread(new ThreadStart(callsap));
lsThread.IsBackground = true;
lsThread.Start();
}
void callsap()
{
string connectionString = "AppServerHost=10.XXX.XXX.XXX; SystemNumber=00; User=XXXXXX;Password=XXXXXX; Client=XXXX; Language=ZH; PoolSize=5; Trace=8";
var connection = new SapConnection(connectionString);
connection.Connect();
toolStripStatusLabel1.Text = "Logon SAP Server...";
var someFunction = connection.CreateFunction("ZSAP_CU50_OUT");
var result = someFunction.Invoke<return_table>(new SomeFunctionParameters
{
WERKS = "XXXXX",
MATNR = "XXXXX",
BADAT = "20240126",
PM = "XXXXX",
CC = "CC12",
PA = "PA60",
PKG = "XXXXX",
});
toolStripStatusLabel1.Text = "BOM:" + result.Itemsa.Count().ToString() + " CFG:" + result.Itemsb.Count().ToString();
listView1.BeginUpdate();
listView2.BeginUpdate();
foreach (var one in result.Itemsa)
{
ListViewItem item = new ListViewItem(one.Z_BOM_LINE);
item.SubItems.Add(one.TEXT);
item.SubItems.Add(one.MATNR);
item.SubItems.Add(one.QUANTITY);
item.SubItems.Add(one.MEIN);
item.SubItems.Add(one.WORKSTATION);
item.SubItems.Add(one.Z_TYPE);
listView1.Items.Add(item);
}
foreach (var one in result.Itemsb)
{
ListViewItem item = new ListViewItem(one.MNAME);
item.SubItems.Add(one.ATBEZ);
item.SubItems.Add(one.MWERT);
item.SubItems.Add(one.ATWTB);
listView2.Items.Add(item);
}
listView1.EndUpdate();
listView2.EndUpdate();
}
}
class return_table
{
[SapName("OT_BOM_LIST")]
public TableAItem[] Itemsa { get; set; }
[SapName("OT_BOM_CFG")]
public TableBItem[] Itemsb { get; set; }
}
class TableAItem
{
[SapName("Z_BOM_LINE")]
public string Z_BOM_LINE { get; set; }
[SapName("TEXT")]
public string TEXT { get; set; }
[SapName("MATNR")]
public string MATNR { get; set; }
[SapName("QUANTITY")]
public string QUANTITY { get; set; }
[SapName("MEIN")]
public string MEIN { get; set; }
[SapName("WORKSTATION")]
public string WORKSTATION { get; set; }
[SapName("Z_TYPE")]
public string Z_TYPE { get; set; }
}
class TableBItem
{
[SapName("MNAME")]
public string MNAME { get; set; }
[SapName("ATBEZ")]
public string ATBEZ { get; set; }
[SapName("MWERT")]
public string MWERT { get; set; }
[SapName("ATWTB")]
public string ATWTB { get; set; }
}
class SomeFunctionParameters
{
[SapName("WERKS")]
public string WERKS { get; set; }
[SapName("MATNR")]
public string MATNR { get; set; }
[SapName("BADAT")]
public string BADAT { get; set; }
[SapName("PM")]
public string PM { get; set; }
[SapName("CC")]
public string CC { get; set; }
[SapName("PA")]
public string PA { get; set; }
[SapName("PKG")]
public string PKG { get; set; }
}
}