若需要從SAP中取出data, 除了用web service 外, 可以直接call其BAPI call. 方法如下:
SapDestinationConfiguration.cs
using SAP.Middleware.Connector; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Test.Poc.Sap.Library { public class SapDestinationConfiguration : IDestinationConfiguration { public event RfcDestinationManager.ConfigurationChangeHandler ConfigurationChanged; public bool ChangeEventsSupported() { return false; } public RfcConfigParameters GetParameters(string destinationName) { RfcConfigParameters param = new RfcConfigParameters(); param.Add(RfcConfigParameters.Name, "MM"); param.Add(RfcConfigParameters.AppServerHost, "10.10.28.172"); param.Add(RfcConfigParameters.SystemNumber, "00"); param.Add(RfcConfigParameters.SystemID, "NAD"); param.Add(RfcConfigParameters.User, "sapna01"); param.Add(RfcConfigParameters.Password, "abcd1234"); param.Add(RfcConfigParameters.Client, "502"); param.Add(RfcConfigParameters.Language, "EN"); param.Add(RfcConfigParameters.PoolSize, "10"); return param; } } }
SapConnector.cs
using Test.Poc.Sap.Library.Bases; using SAP.Middleware.Connector; using SPEED.SAPConnection; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Test.Poc.Sap.Library { public class SapConnector : IConnector { private bool isConnected; public bool IsConnected { get { return isConnected; } set { isConnected = value; } } private RfcDestination rfcDestination; public RfcDestination RfcDestination { get { return rfcDestination; } set { rfcDestination = value; } } public bool Connect() { bool result = false; string destinationConfigurationName = "MM"; IDestinationConfiguration destinationConfig = null; bool isDestinationInitialize = false; try { if (!isDestinationInitialize) { destinationConfig = new SapDestinationConfiguration(); destinationConfig.GetParameters(destinationConfigurationName); if (RfcDestinationManager.TryGetDestination(destinationConfigurationName) == null) { RfcDestinationManager.RegisterDestinationConfiguration(destinationConfig); isDestinationInitialize = true; this.IsConnected = true; result = true; } else { this.IsConnected = false; } if (rfcDestination == null) { rfcDestination = RfcDestinationManager.GetDestination("MM"); } rfcDestination.Ping(); } } catch (Exception ex) { throw (ex); } return result; } public bool Disconnect() { rfcDestination= null; return true; } public bool testConnection() { bool result = false; try { if(this.rfcDestination==null) rfcDestination = RfcDestinationManager.GetDestination("MM"); rfcDestination.Ping(); result = true; } catch (Exception ex) { throw(ex); } return result; } } }
SapExecutor.cs
using Test.Poc.Sap.Library.Bases; using SAP.Middleware.Connector; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Test.Poc.Sap.Library { public class SapExecuter : BaseExecuter { public SapExecuter(SapConnector sapConnector): base(sapConnector) { } public override object Execute() { DataSet result = new DataSet(); RfcDestination rfcDestination = ((SapConnector)connector).RfcDestination; RfcRepository rfcRepository = rfcDestination.Repository; IRfcFunction rfcFunction = rfcRepository.CreateFunction("BAPI_PO_GETITEMSREL"); rfcFunction.SetValue("REL_GROUP", "Z2"); rfcFunction.SetValue("REL_CODE", "5"); rfcFunction.Invoke(rfcDestination); String names = ""; for (int i = 1; i < rfcFunction.Count; i++) { names += rfcFunction.GetElementMetadata(i).Name+", "; IRfcTable customerData = rfcFunction.GetTable("RETURN"); result.Tables.Add(ConvertToDataTable(customerData)); } //IRfcTable customerSummary = rfcDestination.Repository.GetTableMetadata("ZIRM_CUSTOMER_SUMMARY").CreateTable(); //customerSummary = customerData.GetTable("CUSTOMER_DATA"); //result.Tables.Add(this.ConvertToDataTable(customerSummary)); return result; } private DataTable ConvertToDataTable(IRfcTable rfcTable) { DataTable dataTable = new DataTable(); // Add columns. for (int item = 0; item < rfcTable.ElementCount; item++) { RfcElementMetadata metaData = rfcTable.GetElementMetadata(item); dataTable.Columns.Add(metaData.Name); } foreach (IRfcStructure row in rfcTable) { DataRow dataRow = dataTable.NewRow(); for(int item=0; item<rfcTable.ElementCount; item++) { RfcElementMetadata metaData = rfcTable.GetElementMetadata(item); if ((metaData.DataType == RfcDataType.BCD) && (metaData.Name=="ABC")) dataRow[item] = row.GetInt(metaData.Name); else dataRow[item] = row.GetString(metaData.Name); } dataTable.Rows.Add(dataRow); } return dataTable; } } }
Reference:
- SAP Integration with .Net 4.0 (SAP Connection Manager), http://www.codeproject.com/Tips/521485/SAP-Integration-with-Net-SAP-Connection-Manage
- SAP .Net Connector 3.0 (NCo) Example,
http://www.codeproject.com/Articles/824928/SAP-Net-Connector-NCo-Example
Leave a Reply