SAP接口编程-RFC系列12 : Table Parameter作为输出参数

本篇演示 Table parameter 作为出参时的处理方法。要使用的函数是 BAPI_COMPANYCODE_GETLIST

首先,添加加 SAP Table Factory ActiveX 控件的引用:在 Project 中添加 SAP Table Factory ActiveX 控件的引用。并且在代码中 using SAPTableFactoryCtrl;

为方便程序使用,将 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();
            }
        }
    }
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值