玄铁剑

成功的途径:抄,创造,研究,发明...
posts - 128, comments - 42, trackbacks - 0, articles - 174

.NET调用SAP RFC

Posted on 2009-11-25 19:12 玄铁剑 阅读(566) 评论(0)  编辑 收藏 引用 所属分类: SAP

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Microsoft.Adapter.SAP;

namespace ConsoleForSap
{
    public class SapConfig
    {
        public string ASHOST { get; set; }
        public string CLIENT { get; set; }
        public string LANG { get; set; }
        public string USER { get; set; }
        public string PASSWD { get; set; }
        public int SYSNR { get; set; }
    }

    public class SapParameter
    {
        public string ParamName { get; set; }
        public object ParamValue { get; set; }
        private ArrayList ObjSapParam = new ArrayList();

        public void AddParam(string ParamName, object ParamValue)
        {
            SapParameter param = new SapParameter();
            param.ParamName = ParamName;
            param.ParamValue = ParamValue;
            ObjSapParam.Add(param);
        }

        public void ClearParam()
        {
            ObjSapParam.Clear();
        }

        public Object[] GetParam()
        {
            return ObjSapParam.ToArray();
        }
    }

    public class CallSAP
    {
        private DataSet MapSapData(SAPDataReader Sapdr)
        {
            DataSet ds = new DataSet();
            DataTable dtResult;
            DataTable dtSchema;
            do
            {
                dtResult = new DataTable();
                dtSchema = Sapdr.GetSchemaTable();
                foreach (DataRow dr in dtSchema.Rows)
                {
                    DataColumn col = new DataColumn();
                    col.ColumnName = dr["ColumnName"].ToString();
                    col.Caption = dr["Caption"].ToString();
                    col.DataType = System.Type.GetType(dr["DataType"].ToString());
                    if (dr["DataType"].ToString().ToLower().EndsWith(".system") == true)
                    {
                        int iSize = 0;
                        int.TryParse(dr["ColumnSize"].ToString(), out iSize);
                        col.MaxLength = iSize;
                    }
                    dtResult.Columns.Add(col);
                }
                while (Sapdr.Read())
                {
                    DataRow dr = dtResult.NewRow();
                    for (int i = 0; i < Sapdr.FieldCount; i++)
                    {
                        try
                        {
                            dr[i] = Sapdr.GetValue(i);
                        }
                        catch { }
                    }
                    dtResult.Rows.Add(dr);
                }
                ds.Tables.Add(dtResult);
            }
            while (Sapdr.NextResult());
            return ds;
        }

        public DataSet ExecuteSapCmd(SapConfig conf, string SapRfcName, SapParameter ObjParam, ref string strMsg)
        {
            string strConn = "ASHOST=" + conf.ASHOST + "; CLIENT=" + conf.CLIENT + ";SYSNR=" + conf.SYSNR.ToString() + ";USER=" + conf.USER + ";PASSWD=" + conf.PASSWD + ";LANG=" + conf.LANG;
            return ExecuteSapCmd(strConn, SapRfcName, ObjParam, ref strMsg);
        }

        public DataSet ExecuteSapCmd(string strConn, string SapRfcName, SapParameter ObjParam, ref string strMsg)
        {
            DataSet dsOut = new DataSet();
            try
            {
                strMsg = "ok";
                SAPConnection con = new SAPConnection(strConn);
                con.Open();
                SAPCommand cmd = new SAPCommand(con);
                string strCmd = "EXEC " + SapRfcName + " ";
                foreach (SapParameter obj in ObjParam.GetParam())
                {
                    if (string.IsNullOrEmpty(obj.ParamName) == true)
                    {
                        strCmd += "'" + obj.ParamValue + "',";
                    }
                    else
                    {
                        strCmd += "@" + obj.ParamName + "='" + obj.ParamValue + "',";
                    }
                }
                if (strCmd.EndsWith(",") == true)
                {
                    strCmd = strCmd.Substring(0, strCmd.Length - 1);
                }
                cmd.CommandText = strCmd;
                SAPDataReader drOut = cmd.ExecuteReader();
                dsOut = MapSapData(drOut);
                drOut.Close();
            }
            catch(Exception e)
            {
                strMsg = e.Message;
            }
            return dsOut;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
           string strMsg = "";
           SapConfig cof = new SapConfig();
           cof.ASHOST = "192.168.0.1";
           cof.CLIENT = "20";
           cof.LANG = "ZF";
           cof.USER = "sap";
           cof.PASSWD = "sap";
           cof.SYSNR = 0;

           SapParameter param = new SapParameter();
           param.AddParam("PartNo", "PN6022");
           param.AddParam("Factory", "WBA");
           CallSAP call = new CallSAP();
           DataSet dsBom = call.ExecuteSapCmd(cof, "rfc_Bom", param, ref strMsg);
           WriteData(dsBom, strMsg);

           param.ClearParam();
           param.AddParam("", "01436886");
           DataSet dsSd = call.ExecuteSapCmd(cof, "rfc_Delivery", param, ref strMsg);

           WriteData(dsSd, strMsg);
           Console.WriteLine("Running finished.");
           Console.ReadKey(true);
        }

        static void WriteData(DataSet ds, string strMsg)
        {
            Console.WriteLine("|{0,64:C}|", "-".PadLeft(64, '-'));               
            Console.WriteLine("|{0,64:C}|",(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ")+strMsg).PadRight(64,' '));
            Console.WriteLine("|{0,64:C}|", "-".PadLeft(64, '-'));               
            foreach (DataTable dt in ds.Tables)
            {
                Console.WriteLine("|{0,12:C}|{1,12:C}|{2,12:C}|{3,12:C}|{4,12:C}|", dt.Columns[0].Caption, dt.Columns[1].Caption,
                    dt.Columns[2].Caption, dt.Columns[3].Caption,dt.Columns[4].Caption);
                Console.WriteLine("|{0,64:C}|", "-".PadLeft(64, '-'));               
                foreach (DataRow dr in dt.Rows)
                {
                    Console.WriteLine(String.Format("|{0,12:C}|{1,12:C}|{2,12:C}|{3,12:C}|{4,12:C}|", dr[0], dr[1], dr[2], dr[3], dr[4]));
                }
                Console.WriteLine("|{0,64:C}|","-".PadLeft(64,'-'));               
            }
        }
    }
}


DataProviderForSAP

只有注册用户登录后才能发表评论。