SAP接口編程之 RFC 系列(13) : C# 版 Table 作為輸入?yún)?shù)

在之前 VBA 代碼中,我們已經(jīng)介紹過 table parameter 作為輸入?yún)?shù)的要點(diǎn)。本篇主要演示 C# 使用 table parameter 作為 importing parameter 的語法。以 RFC_READ_TABLE 函數(shù)為例。

using System.Data;
using SAPLogonCtrl;
using SAPFunctionsOCX;
using ConnectionProvider;
using SAPTableFactoryCtrl;


// Table parameter 作為 importing parameter 時(shí)的使用方法
namespace SAPRfcCall
{
    public class RFC3
    {
        private Connection connection;

        // get table SKA1 contents
        public DataTable GetSKA1()
        {
            DataTable ska1 = null;

            bool isSuccessful = SAPConnection.SilentLogon(
                "192.168.65.100", "D01", 00, "001", "STONE", "sappwd", "ZH");

            if (isSuccessful) {
                connection = SAPConnection.Connection;
                ska1 = DoGetTableLines();
                SAPConnection.Logoff();
            }

            return ska1;
        }

        public DataTable DoGetTableLines()
        {
            if (connection.IsConnected != CRfcConnectionStatus.tloRfcConnected) {
                return null;
            }

            SAPFunctions functions = new SAPFunctions();
            functions.Connection = connection;
            Function fm = functions.Add("RFC_READ_TABLE");

            fm.Exports["QUERY_TABLE"].Value = "SKA1";
            fm.Exports["DELIMITER"].Value = "~";

            // criterial to filter data retrieved
            Tables tables = fm.Tables;
            Table options = tables.get_Item("OPTIONS");
            options.AppendGridData(1, 1, 1, "KTOPL = 'Z900' ");

            // only cares for two fields
            Table fields = tables.get_Item("FIELDS");
            fields.AppendGridData(1, 1, 1, "KTOPL"); // first row
            fields.AppendGridData(2, 1, 1, "SAKNR"); // second row

            fm.Call();

            Table data = fm.Tables["DATA"];
            DataTable tableLines = Utils.ToDataTable(data);

            return tableLines;
        }
    }
}

說明: 向 table 參數(shù)添加行時(shí),與 VBA 語法不同,使用AppendGridData() 方法。 另外,獲取 importing 參數(shù)、exporting 參數(shù)、table 參數(shù),可以有以下兩種方法:

  • 方法1
fm.get_Exports("QUERY_TABLE").Value = "SKA1";
// 或者
fm.Exports["QUERY_TABLE"].Value = "SKA1"
  • 方法2
Tables tables = fm.Tables;
Table options = tables.get_Item("OPTIONS");
// or : Table options = fm.Tables["OPTIONS"];

單元測(cè)試

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SAPRfcCall;
using System.Data;

namespace UnitTestProject
{
    [TestClass]
    public class TestRFC3
    {
        [TestMethod]
        public void TestRFCReadTable()
        {
            RFC3 rfc = new RFC3();
            DataTable ska1 = rfc.GetSKA1();
            Utils.ConsolePrint(ska1);
        }
    }
}

Utils.ConsolePrint() 方法在 Output 窗口打印 dataTable,代碼如下:

public static void ConsolePrint(DataTable dataTable)
{
    // function: print dataTable contents in console

    foreach (DataRow row in dataTable.Rows) {
        foreach (DataColumn col in dataTable.Columns) {
            Console.Write(row[col.Caption].ToString() + "\t");
        }
        Console.WriteLine();
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容