要點(diǎn)講解
如果table作為輸入?yún)?shù)楞陷,需要用戶填充每一行的內(nèi)容。用法說(shuō)明如下茉唉。我們?nèi)匀灰訰FC_READ_TABLE這個(gè)函數(shù)為例固蛾。關(guān)于函數(shù)參數(shù)的說(shuō)明,請(qǐng)參照本系列之前的文檔度陆,或自行g(shù)oogle艾凯。
接著上一篇的文檔和代碼,使用RFC_READ_TABLE函數(shù)讀取SKA1表科目表為Z900的數(shù)據(jù)懂傀,輸出KTOPL和SAKNR這兩個(gè)字段趾诗。
在TableManipulation.cs中增加一個(gè)ReadTable方法:
public DataTable ReadTable()
{
// purpose:
// read table SKA1 with criteria: KTOPL = Z900
// and returns a DataTable
RfcDestination dest = DestinationProvider.GetDestination();
IRfcFunction fm = dest.Repository.CreateFunction("RFC_READ_TABLE");
fm.SetValue("QUERY_TABLE", "SKA1");
fm.SetValue("DELIMITER", "~");
// OPTIONS table parameter
IRfcTable options = fm.GetTable("OPTIONS");
options.Append(); // create a new row
options.CurrentRow.SetValue(0, "KTOPL = 'Z900' ");
// FIELDS table parameter
IRfcTable fields = fm.GetTable("FIELDS");
fields.Append();
fields.CurrentRow.SetValue(0, "KTOPL");
fields.Append();
fields.CurrentRow.SetValue(0, "SAKNR");
fm.Invoke(dest);
// DATA table paramter (output)
IRfcTable data = fm.GetTable("DATA");
DataTable dTable = Utils.ToDataTable(data);
return dTable;
}
options這個(gè)table parameter用于設(shè)置數(shù)據(jù)選擇的條件。我們使用的條件是KTOPL = 'Z900'蹬蚁。注意相關(guān)代碼片段:
IRfcTable options = fm.GetTable("OPTIONS");
options.Append(); // create a new row
options.CurrentRow.SetValue(0, "KTOPL = 'Z900' ");
options.CurrentRow返回的是一個(gè)IRfcStructure恃泪,注意現(xiàn)在的代碼是根據(jù)位置索引(從0開(kāi)始)來(lái)設(shè)置elemnet的值。如果我們使用SE37查看RFC_READ_TABLE的參數(shù)犀斋,我們可以看到options表參數(shù)類型為RFC_DB_OPT贝乎,只有一個(gè)字段:TEXT。NCo3.0的SetXXX()方法和GetXXX()方法一樣叽粹,既可以通過(guò)索引览效,也可以通過(guò)字段名蒙具。所以代碼也可以這樣:
IRfcTable options = fm.GetTable("OPTIONS");
options.Append(); // create a new row
options.CurrentRow.SetValue("TEXT", "KTOPL = 'Z900' ");
當(dāng)然,因?yàn)镹Co3.0內(nèi)部通過(guò)CurrentIndex來(lái)標(biāo)記當(dāng)前行朽肥,所以代碼可以進(jìn)一步簡(jiǎn)化為這樣(可讀性不好):
IRfcTable options = fm.GetTable("OPTIONS");
options.Append(); // create a new row
options.SetValue("TEXT", "KTOPL = 'Z900' ");
完整代碼
// File name: TableManipulation.cs
/**
* Author: Stone Wang(stone.wangmin@qq.com)
* Date: 2016/3/26
*/
using SAP.Middleware.Connector;
using NCo02;
using System.Data;
namespace NCo03
{
public class TableManipulation
{
public DataTable ReadTable()
{
// purpose:
// read table SKA1 with criteria: KTOPL = Z900
// and returns a DataTable
RfcDestination dest = DestinationProvider.GetDestination();
IRfcFunction fm = dest.Repository.CreateFunction("RFC_READ_TABLE");
fm.SetValue("QUERY_TABLE", "SKA1");
fm.SetValue("DELIMITER", "~");
// OPTIONS table parameter
IRfcTable options = fm.GetTable("OPTIONS");
options.Append(); // create a new row
options.SetValue("TEXT", "KTOPL = 'Z900' ");
// FIELDS table parameter
IRfcTable fields = fm.GetTable("FIELDS");
fields.Append();
fields.CurrentRow.SetValue("FIELDNAME", "KTOPL");
fields.Append();
fields.CurrentRow.SetValue("FIELDNAME", "SAKNR");
fm.Invoke(dest);
// DATA table paramter (output)
IRfcTable data = fm.GetTable("DATA");
DataTable dTable = Utils.ToDataTable(data);
return dTable;
}
}
}
單元測(cè)試
// File name: TestTableManipulation.cs
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Data;
using NCo03;
namespace UnitTestProject1
{
[TestClass]
public class TestTableManipulation
{
[TestMethod]
public void Test_ReadTable()
{
TableManipulation rfc = new TableManipulation();
DataTable ska1 = rfc.ReadTable(); // ska1 table
Utils.PrintDataTable(ska1);
}
}
}