C#调用abap RFC函数,返回表数据

Form1.cs


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SAP.Middleware.Connector;
using System.Data.Sql;
using System.Configuration;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

          


        }

        public DataTable GetSapData()
        {
            DataTable Dtable = new DataTable();
            try
            {
                RfcDestination rfcDestination = RfcDestinationManager.GetDestination("EP1");
                RfcRepository rfcRepository = rfcDestination.Repository;
                IRfcFunction myfun = rfcRepository.CreateFunction("ZRFC_GET_MARA_DATA");

                IRfcStructure import = null;


               // import = rfcRepository.GetStructureMetadata("TAB_MARA").CreateStructure();

                myfun.SetValue("I_MATNR", 2000);
              
                myfun.Invoke(rfcDestination);

                IRfcTable tables = myfun.GetTable("ET_MARA");
                Dtable = ConverToDoNetTable(tables);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return Dtable;
        }

        private DataTable ConverToDoNetTable(IRfcTable RFCTable)
        {
            DataTable dtTable = new DataTable();
            //Create table
            for (int item = 0; item < RFCTable.ElementCount; item++)
            {
                RfcElementMetadata metadata = RFCTable.GetElementMetadata(item);
                dtTable.Columns.Add(metadata.Name);
            }

            foreach (IRfcStructure row in RFCTable)
            {
                DataRow dr = dtTable.NewRow();
                for (int item = 0; item < RFCTable.ElementCount; item++)
                {
                    RfcElementMetadata metadata = RFCTable.GetElementMetadata(item);
                    if (metadata.DataType == RfcDataType.BCD)
                    {
                        dr[item] = row.GetDouble(metadata.Name);
                    }
                    else
                    {
                        dr[item] = row.GetString(metadata.Name);
                    }
                }
                dtTable.Rows.Add(dr);
            }
            return dtTable;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
          dt =  GetSapData();
          foreach (DataRow r in dt.Rows)
          {
              MessageBox.Show(r["MATNR"].ToString());
              return;
          }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt = GetSapData();
            dataGridView2.DataSource = dt;
            foreach (DataRow r in dt.Rows)
            {
                //MessageBox.Show(r["MATNR"].ToString());
                //return;
            }
        }

       

       
    }
}

web.config
image

image


FUNCTION ZRFC_GET_MARA_DATA.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     REFERENCE(I_MATNR) TYPE  MARA-MATNR
*"  EXPORTING
*"     REFERENCE(E_RETURN) TYPE  BAPIRET2
*"  TABLES
*"      ET_MARA STRUCTURE  MARA
*"----------------------------------------------------------------------

REFRESH ET_MARA.

  IF I_MATNR IS INITIAL.
    SELECT * FROM MARA
      INTO TABLE ET_MARA.
  ELSE.
    SELECT * FROM MARA
      INTO TABLE ET_MARA  UP TO 50 ROWS ORDER BY ERSDA DESCENDING .

      "WHERE MATNR = I_MATNR.
  ENDIF.

  IF SY-SUBRC = 0.
    E_RETURN-TYPE   = 'S'.          " 成功
    E_RETURN-MESSAGE = '查询成功'.   " 消息文本
  ELSE.
    E_RETURN-TYPE   = 'E'.          " 错误
    E_RETURN-MESSAGE = '未找到数据'.
  ENDIF.



ENDFUNCTION.
posted @ 2026-08-19 17:25  虚拟过客之IT老男人  阅读(7)  评论(0)    收藏  举报