SAP接口编程之 NCo3.0系列(05) : Table作为输入参数
2017-01-07 00:41
465 查看
本系列由stoneWM原创,在CSDN和简书发布。
接着上一篇的文档和代码,使用RFC_READ_TABLE函数读取SKA1表科目表为Z900的数据,输出KTOPL和SAKNR这两个字段。
在TableManipulation.cs中增加一个ReadTable方法:
options这个table parameter用于设置数据选择的条件。我们使用的条件是KTOPL = ‘Z900’。注意相关代码片段:
options.CurrentRow返回的是一个IRfcStructure,注意现在的代码是根据位置索引(从0开始)来设置elemnet的值。如果我们使用SE37查看RFC_READ_TABLE的参数,我们可以看到options表参数类型为RFC_DB_OPT,只有一个字段:TEXT。NCo3.0的SetXXX()方法和GetXXX()方法一样,既可以通过索引,也可以通过字段名。所以代码也可以这样:
当然,因为NCo3.0内部通过CurrentIndex来标记当前行,所以代码可以进一步简化为这样(可读性不好):
要点讲解
如果table作为输入参数,需要用户填充每一行的内容。用法说明如下。我们仍然以RFC_READ_TABLE这个函数为例。关于函数参数的说明,请参照本系列之前的文档,或自行google。接着上一篇的文档和代码,使用RFC_READ_TABLE函数读取SKA1表科目表为Z900的数据,输出KTOPL和SAKNR这两个字段。
在TableManipulation.cs中增加一个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这个table parameter用于设置数据选择的条件。我们使用的条件是KTOPL = ‘Z900’。注意相关代码片段:
IRfcTable options = fm.GetTable("OPTIONS"); options.Append(); // create a new row options.CurrentRow.SetValue(0, "KTOPL = 'Z900' ");
options.CurrentRow返回的是一个IRfcStructure,注意现在的代码是根据位置索引(从0开始)来设置elemnet的值。如果我们使用SE37查看RFC_READ_TABLE的参数,我们可以看到options表参数类型为RFC_DB_OPT,只有一个字段:TEXT。NCo3.0的SetXXX()方法和GetXXX()方法一样,既可以通过索引,也可以通过字段名。所以代码也可以这样:
IRfcTable options = fm.GetTable("OPTIONS"); options.Append(); // create a new row options.CurrentRow.SetValue("TEXT", "KTOPL = 'Z900' ");
当然,因为NCo3.0内部通过CurrentIndex来标记当前行,所以代码可以进一步简化为这样(可读性不好):
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;
}
}
}
单元测试
// 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); } } }
相关文章推荐
- SAP接口编程之 NCo3.0系列(04) : Table作为输出参数
- SAP接口编程-RFC系列06 : Table作为输入参数
- SAP接口编程-RFC系列12 : Table Parameter作为输出参数
- SAP接口编程 之 JCo3.0系列(03) : Table参数
- SAP接口编程 之 JCo3.0系列(03) : Table参数
- SAP接口编程-RFC系列13 : Table Parameter作为输入参数
- SAP接口编程-Nco3.0系列01 : RfcDestination
- SAP接口编程-RFC系列05 : Table作为输出参数
- SAP接口编程 之 JCo3.0系列(05) : Exception Handling
- SAP接口编程之 NCo3.0系列(03) : Meta-data和Container
- SAP接口编程之 NCo3.0系列(06) : 会话管理
- SAP接口编程 之 JCo3.0系列(05) : Exception Handling
- SAP接口编程之 NCo3.0系列(07) : 异常处理
- SAP接口编程之 NCo3.0系列(02) : RFC Client Programs
- SAP接口编程-RFC系列10 : BAPI控件的DimAs方法
- Table-values parameter(TVP)系列之二: 利用DataTable将其作为参数传给SP
- Table-values parameter(TVP)系列之二: 利用DataTable将其作为参数传给SP
- Table-values parameter(TVP)系列之三: 利用Collection将其作为参数传给SP
- SAP接口编程-RFC系列14 : 获取SAP DDIC的数据字段
- Table-values parameter(TVP)系列之二: 利用DataTable将其作为参数传给SP