本文主要是介绍SAP接口编程-RFC系列13 : Table Parameter作为输入参数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在之前VBA代码中,我们已经介绍过table parameter作为输入参数的要点。本篇主要演示c#使用table parameter作为importing parameter的语法。以RFC_READ_TABLE为例。
using System.Data;
using SAPLogonCtrl;
using SAPFunctionsOCX;
using ConnectionProvider;
using SAPTableFactoryCtrl;// Table parameter作为importing parameter时的使用方法
namespace SAPRfcCall
{public class RFC3{private Connection connection;// get table SKA1 contentspublic 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 retrievedTables tables = fm.Tables;Table options = tables.get_Item("OPTIONS");options.AppendGridData(1, 1, 1, "KTOPL = 'Z900' ");// only cares for two fieldsTable fields = tables.get_Item("FIELDS");fields.AppendGridData(1, 1, 1, "KTOPL"); // first rowfields.AppendGridData(2, 1, 1, "SAKNR"); // second rowfm.Call();Table data = fm.Tables["DATA"];DataTable tableLines = Utils.ToDataTable(data);return tableLines;}}
}
说明:
向table参数添加行时,与VBA语法不同,使用AppendGridData()方法。
另外,获取importing参数、exporting参数、table参数,可以有以下两种方法:
fm.get_Exports("QUERY_TABLE").Value = "SKA1";// 或者
fm.Exports["QUERY_TABLE"].Value = "SKA1"
Tables tables = fm.Tables;
Table options = tables.get_Item("OPTIONS");
// or : Table options = fm.Tables["OPTIONS"];
单元测试
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 consoleforeach (DataRow row in dataTable.Rows) {foreach (DataColumn col in dataTable.Columns) {Console.Write(row[col.Caption].ToString() + "\t");}Console.WriteLine();}
}
VBA版对照:http://blog.csdn.net/stone0823/article/details/50329257
这篇关于SAP接口编程-RFC系列13 : Table Parameter作为输入参数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!