本篇演示 Table parameter 作为输出参数时的处理方法。要用到的函数是 BAPI_COMPANYCODE_GETLIST
。
- 项目中添加 SAP Table Factory ActiveX 控件的引用
在 Project 中添加 SAP Table Factory ActiveX 控件的引用。并且在代码中导入对应的 namespace:using SAPTableFactoryCtrl;
- 将
SAPTableFactoryCtrl.Table
转换成 Ado.Net 的DataTable
:
为方便程序使用,将SAPTableFactoryCtrl.Table
转换成 Ado.Net
DataTable
。
using System.Data;
namespace SAPRfcCall
{
public class Utils
{
public static DataTable ToDataTable(SAPTableFactoryCtrl.Table itab)
{
DataTable dt = new DataTable();
// Header
for (int col = 1; col <= itab.ColumnCount; col++) {
dt.Columns.Add(itab.Columns[col].Name, typeof(string));
}
// Line items
for (int row = 1; row <= itab.RowCount; row++) {
DataRow dr = dt.NewRow();
for (int col = 1; col <= itab.ColumnCount; col++) {
dr[col - 1] = itab.get_Cell(row, col);
}
dt.Rows.Add(dr);
}
return dt;
}
}
}
RFC 调用:
using System.Data;
using SAPLogonCtrl;
using SAPFunctionsOCX;
using SAPTableFactoryCtrl;
using ConnectionProvider;
namespace SAPRfcCall
{
public class RFC2
{
private Connection connection;
public DataTable GetCompanyCodes()
{
DataTable cocdDataTable = null;
bool isSuccessful = SAPConnection.SilentLogon(
"192.168.65.100", "D01", 00, "001", "STONE", "pwdxxx", "ZH");
if (isSuccessful) {
connection = SAPConnection.Connection;
cocdDataTable = DoGetCompanyCodes();
SAPConnection.Logoff();
}
return cocdDataTable;
}
public DataTable DoGetCompanyCodes()
{
if (connection.IsConnected != CRfcConnectionStatus.tloRfcConnected) {
return null;
}
SAPFunctions functions = new SAPFunctions();
functions.Connection = connection;
Function fm = functions.Add("BAPI_COMPANYCODE_GETLIST");
fm.Call();
Table cocdDataTable = fm.Tables["COMPANYCODE_LIST"];
DataTable dt = Utils.ToDataTable(cocdDataTable);
return dt;
}
}
}
单元测试:
using System;
using System.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SAPRfcCall;
namespace UnitTestProject
{
[TestClass]
public class TestRFC2
{
[TestMethod]
public void TestGetCompanyCodes()
{
RFC2 rfc = new RFC2();
DataTable cocdDataTable = rfc.GetCompanyCodes();
foreach (DataRow row in cocdDataTable.Rows) {
foreach (DataColumn col in cocdDataTable.Columns) {
Console.Write(row[col.Caption].ToString() + "\t");
}
Console.WriteLine();
}
}
}
}