cto67 发表于 2015-9-17 10:58:19

C# 在項目中使用SAP

  1.添加引用 sapnco.dll
  此文件時SAP的一個類庫,版本是3.0
  2.在web.config中進行配置
  <appSettings>
      <add key="Type" value="3"/>
      <add key="NAME" value="DEV"/>
      <add key="ASHOST" value="127.0.0.1"/> //SAP地址
      <add key="SYSNR" value="00"/>
      <add key="CLIENT" value="600"/>
      <add key="Language" value="EN"/> //使用版本
      <add key="USER" value="AAA"/> //登陸用戶名
      <add key="PASSWD" value="BBB"/>// 登陸密碼
      </appSettings>
  3.添加一個SAPSeverInfo,存放web.config中信息
  代碼如下:


View Code


public   class SAPSeverInfo
    {
      public string NAME { set; get; }
      public string ASHOST { set; get; }
      public string SYSNR { set; get; }
      public string CLIENT { set; get; }
      public string LANG { set; get; }
      public string USER { set; get; }
      public string PASSWD { set; get; }
      public string Type { set; get; }
    }
  4.添加初始化SAP基本信息的類SapConfig
  代碼如下:


View Code


public class SapConfig : IDestinationConfiguration
{

      #region IDestinationConfiguration 成員
      private SAPSeverInfo info;
      public SapConfig(SAPSeverInfo info) {
          this.info = info;
      }
      public RfcConfigParameters GetParameters(string destinationName)
      {
          try
          {
            if ("PRD".Equals(destinationName))
            {
                  RfcConfigParameters parms = new RfcConfigParameters();
                  parms.Add(RfcConfigParameters.AppServerHost, info.ASHOST);
                  parms.Add(RfcConfigParameters.Client, info.CLIENT);
                  parms.Add(RfcConfigParameters.User, info.USER);
                  parms.Add(RfcConfigParameters.Password, info.PASSWD);
                  parms.Add(RfcConfigParameters.Language, info.LANG);
                  parms.Add(RfcConfigParameters.SystemNumber, info.SYSNR);
                  return parms;
            }
            else
            {
                  return null;
            }
          }
          catch (Exception ex)
          {
            return null;
          }
      }
      #endregion
      public bool ChangeEventsSupported()
      {
          return false;
      }

      public event RfcDestinationManager.ConfigurationChangeHandler ConfigurationChanged;

}
  5.定義SAP操作類SAPServerConnector
  代碼如下:


View Code


publicclass SAPServerConnector:IDisposable
    {
       private SAPSeverInfo sapInfo = null;   
       private RfcDestination prd =null;
       private SapConfig sapConfig = null;
       /// <summary>
       /// 晦獄 儅撩濠
       /// </summary>
       public SAPServerConnector()
       {
         // SAP 憮幗 翱唸 僥濠翮擊 儅撩朝 Helper 贗楚蝶 偃羹蒂 儅撩棻.
         try
         {
         //if (RfcDestinationManager.GetDestination("PRD") == null)
         //{
               this.sapInfo = Common.GetSapServerInfo();
               sapConfig = new SapConfig(this.sapInfo);
                   SAP.Middleware.Connector.RfcDestinationManager.RegisterDestinationConfiguration(sapConfig);
         //}
               prd = RfcDestinationManager.GetDestination("PRD");
         }
         catch (Exception ex)
         {
               return;
         }
       }

       public DataTable getSapFunctionToTable(string funName, Dictionary<string, string> lstParameters
                              , string TableName,IRfcTable rtbIput,stringtableindex
                              , Dictionary<string, string> outParameters
                              ,List<string> outList)
       {
         try
         {
               DataTable dt = new DataTable();
               RfcRepository repo = prd.Repository;
               IRfcFunction Z_RFC_ZCOX = repo.CreateFunction(funName);
               foreach (var item in lstParameters)
               {
                   Z_RFC_ZCOX.SetValue(item.Key, item.Value);
               }
               Z_RFC_ZCOX.SetValue(tableindex, rtbIput);
               Z_RFC_ZCOX.Invoke(prd);
               IRfcTable rtb = Z_RFC_ZCOX.GetTable(TableName);
               foreach (var item in outList)
                {
                  outParameters = Z_RFC_ZCOX.GetString(item);
                }
               dt = CreateTable(rtb, TableName);
               return dt;
         }
         catch (Exception ex)
         {
               return null;
               //throw new Exception(ex.Message);
         }
       }

       /// <summary>
       /// 根据返回的数据,生成datatable
       /// </summary>
       /// <param name="rtb"></param>
       /// <param name="TableName"></param>
       /// <returns></returns>
         public DataTable CreateTable(IRfcTable rtb,string TableName)
      {
            DataTable dt = new DataTable(TableName);

            for (int i = 0; i <= rtb.ElementCount - 1; i++)
            {
                System.Data.DataColumn dcl = new DataColumn(rtb.GetElementMetadata(i).Name);
                dt.Columns.Add(dcl);
            }
            for (int k = 0; k <= rtb.RowCount - 1; k++)
            {
                DataRow dr = dt.NewRow();
                for (int i = 0; i <= rtb.ElementCount - 1; i++)
                {
                  dr = rtb.GetValue();
                }
                dt.Rows.Add(dr);
            }
            return dt;
      }
         public IRfcTable CreateRfcTable(DataTable dt,string funName, string TableName)
         {
             try
             {
               RfcRepository repo = prd.Repository;
               IRfcFunction Z_RFC_ZCOX = repo.CreateFunction(funName);
               IRfcTable rtb = Z_RFC_ZCOX.GetTable(TableName,true);
               for (int i = 0; i < dt.Rows.Count; i++)
               {
                     rtb.Append();
                     for (int j = 0; j < dt.Columns.Count; j++)
                     {
                         rtb.SetValue(dt.Rows.ToString());
                     }
               }
               return rtb;
             }
             catch (Exception ex)
             {
               return null;
             }
         }

         #region IDisposable 成員
         public void Dispose()
         {
             RfcDestinationManager.UnregisterDestinationConfiguration(sapConfig);
         }
         #endregion
    }
  6.在程序中調用,定義一個 SapOrderM 繼承SAPServerConnector
  代碼如下:


View Code


    class SapOrderM : SAPServerConnector
    {
      public DataTable getFunZ_RFC_CREATE_ORDER_SA(string CodeNo, DataTable ZITMREC, Model.Dealer.DealerDataTable draler
                                                      , Dictionary<string, string> outParameters, string IVDATU, string IKUNWE)
      {
            Dictionary<string, string> lstParameters = new Dictionary<string, string>();
            List<string> outList = new List<string>();
            lstParameters.Add("IKUNNR", draler.VATID);
            lstParameters.Add("IKUNWE", IKUNWE);
            lstParameters.Add("IVKORG", draler.VKORG);
            lstParameters.Add("IVTWEG", draler.VTWEG);
            lstParameters.Add("ISPART", draler.Dept);
            lstParameters.Add("IVKGRP", draler.VKGRP);
            lstParameters.Add("IBSTKD", CodeNo);
            lstParameters.Add("IVDATU", IVDATU);
            outList.Add("OVBELN");
            outList.Add("OAMT");
            outList.Add("OCMSTK");
            outList.Add("OTYPE");
            outList.Add("OMESSAGE");
            outList.Add("ODATUM");
            outList.Add("OUZEIT");
            outList.Add("OLFGSK");
            IRfcTable rtbIput = CreateRfcTable(ZITMREC, "Z_RFC_CREATE_ORDER_SA", "ZITMREC");
            System.Data.DataTable dt = new DataTable();
            dt=getSapFunctionToTable("Z_RFC_CREATE_ORDER_SA", lstParameters, "ZRITMREC"
                  , rtbIput, "ZITMREC", outParameters, outList);
            return dt;
      }}
  SapOrderM類中,也定義了部份要給SAP傳送數據的定義。
  IRfcTable rtbIput = CreateRfcTable(ZITMREC, "Z_RFC_CREATE_ORDER_SA", "ZITMREC");
  這個函數,是把實際的Table數據,轉換成SAP認識的table 。
  同樣,讀取到SAP的數據table,也是需要轉換的。
  如果是字符串,則不需要轉換。
  7.操作SapOrderM類
  A。首先定義傳送給SAP 的數據
  如果需要傳送table數據給SAP


View Code


   #region datatable 数据准备
            DataTable ZITMREC = new DataTable("ZITMREC");
            ZITMREC.Columns.Add("POSNR", Type.GetType("System.String"));
            ZITMREC.Columns.Add("MATNR", Type.GetType("System.String"));
            ZITMREC.Columns.Add("KWMENG", Type.GetType("System.Int16"));
            ZITMREC.Columns.Add("MFLAG", Type.GetType("System.String"));
            for (int i = 0; i < rouseTable.Rows.Count; i++)
            {
                DataRow row = ZITMREC.NewRow();
                row["MATNR"] = rouseTable.Rows["Barcode"].ToString();
                row["KWMENG"] = int.Parse(rouseTable.Rows["Qty"].ToString());
                row["MFLAG"] = "I";
                ZITMREC.Rows.Add(row);
            }
            #endregion
  其中 ZITMREC是 SAP定義的表名
  
页: [1]
查看完整版本: C# 在項目中使用SAP