RFC传出的是汉字,我接收到的都是#号。

解决方案 »

  1.   

          ifunc.Call();  
                        SAPFunctionsOCX.IParameter gPOID = (SAPFunctionsOCX.IParameter)ifunc.get_Imports("L_TITLE");
                        title = System.Text.Encoding.GetEncoding("UTF-8").GetString(System.Text.Encoding.UTF8.GetBytes(gPOID.Value.ToString()));
      

  2.   

    gPOID.Value.ToString()这个汉字出来的都是#号
      

  3.   


    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;
    using SAPLogonCtrl;
    using SAPFunctionsOCX;namespace ArtLee.ChintSAP.SAPClient
    {
        public delegate void SAPEventHandler(object sender,SAPEventArgs e);
        /// <summary>
        /// SAPConnection连接SAPRFC对象
        /// Create By ArtLee
        /// </summary>
        public class SAPConnection:IDisposable
        {
            #region public constructors        public SAPConnection()
            {
                if (!CheckThreadIsSTAThread())
                    throw new SAPException("当前线程不为STA");
                _loginclass = new SAPLogonControlClass();
            }        public SAPConnection(SAPCredential credential)
            {
                if (!CheckThreadIsSTAThread())
                    throw new SAPException("当前线程不为STA");
                _loginclass = new SAPLogonControlClass();
                _credential = credential;
                InitCredential(_credential);
            }        #endregion        #region public events
            /// <summary>
            /// SAP打开连接事件
            /// </summary>
            public event SAPEventHandler SAPOpened;
            /// <summary>
            /// SAP关闭连接事件
            /// </summary>
            public event SAPEventHandler SAPClosed;
            /// <summary>
            /// RFC函数被调用事件
            /// </summary>
            public event SAPEventHandler SAPCallSuccess;        #endregion        #region private varrient        private SAPLogonControlClass _loginclass;        private Connection _currentconnection;        private SAPCredential _credential;        private SAPConnectionState _state;        private SAPFunction _func;        #endregion        #region public properties
            /// <summary>
            /// 获取和设置身份验证
            /// </summary>
            public SAPCredential Credential
            {
                get
                {
                    return _credential;
                }
                set
                {
                    if (null != value)
                    {
                        _credential = value;
                        InitCredential(_credential);
                    }
                }
            }        /// <summary>
            /// 获取Sap连接状态
            /// </summary>
            public SAPConnectionState State
            {
                get { return _state; }
            }        #endregion        #region public methods
            /// <summary>
            /// 打开连接
            /// </summary>
            public void Open()
            {
                _currentconnection = (Connection)_loginclass.NewConnection();
                if (_currentconnection.Logon(0, true))
                {
                    _state = SAPConnectionState.Opened;
                    if (SAPOpened != null)
                        SAPOpened(this, new SAPEventArgs(SAPConnectionState.Opened));
                }
                else
                {
                    _state = SAPConnectionState.Failure;
                    throw new SAPException("Sap连接打开失败");
                }
            }
            /// <summary>
            /// 关闭连接
            /// </summary>
            public void Close()
            {
                if (_state != SAPConnectionState.Closed)
                {
                    _state = SAPConnectionState.Closed;
                    _currentconnection.Logoff();
                    if (SAPClosed != null)
                        SAPClosed(this, new SAPEventArgs(SAPConnectionState.Closed));
                }
                else
                {
                    throw new SAPException("SAPConnection已经关闭");
                }
            }
            /// <summary>
            /// 调用RFC函数
            /// </summary>
            /// <param name="RFC_Name">rfc函数名</param>
            /// <param name="parameters">参数集合包括import和export</param>
            /// <returns></returns>
            public SAPTables InvokeRFC(string rfc_name, SAPParameterCollection parameters)
            {
                try
                {
                    if (State != SAPConnectionState.Opened)
                        throw new SAPException("SAP连接未打开");
                    _func = new SAPFunction(_currentconnection, rfc_name, parameters);
                    _func.Call();
                    if (SAPCallSuccess != null)
                        SAPCallSuccess(this, SAPEventArgs.Empty);
                    return _func.SAPTables;
                }
                catch (SAPException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    throw new SAPException(ex.Message);
                }
            }        #endregion        #region private methods        /// <summary>
            /// 初始化loginclass
            /// </summary>
            /// <param name="credential"></param>
            private void InitCredential(SAPCredential credential)
            {
                _loginclass.ApplicationServer = credential.SapServer;
                _loginclass.Client = credential.Client;
                _loginclass.User = credential.User;
                _loginclass.Password = credential.PassWord;
                _loginclass.SystemNumber = credential.SystemNo;
            }        private void ReleaseCom(object obj)
            {
                if (null != obj)
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
            }
            /// <summary>
            /// 判断线程是否为STA
            /// </summary>
            /// <returns></returns>
            private bool CheckThreadIsSTAThread()
            {
                ApartmentState apartmentstate = Thread.CurrentThread.GetApartmentState();
                return apartmentstate == ApartmentState.STA;
            }        #endregion        #region IDisposable 成员        public void Dispose()
            {
                if (_state != SAPConnectionState.Closed)
                    this.Close();
                _func.Dispose();
                ReleaseCom(_loginclass);
                ReleaseCom(_currentconnection);
            }        #endregion
        }
    }
      

  4.   


    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    using System.Reflection;
    using SAPTableFactoryCtrl;
    namespace ArtLee.ChintSAP.SAPClient
    {
        public class SAPTables
        {
            #region internal constructors        protected internal SAPTables(Tables tables)
            {
                _saptables = tables;
            }        #endregion        #region private variables        private Tables _saptables;        #endregion        #region public properties        public int Count
            {
                get { return _saptables.Count; }
            }        #endregion        #region public methods
            /// <summary>
            /// 根据泛型类型契约自动从RFC提取list
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <returns></returns>
            public List<T> GetList<T>() where T : class, new()
            {
                try
                {
                    if (Count == 0)
                        throw new SAPException("未从RFC函数找到内表或COM+对象已经被释放");
                    SAPContractAttribute attr = SAPReferentHelper.GetClassContract(typeof(T));
                    if (null == attr)
                    {
                        throw new SAPException("类不满足SAP数据契约");
                    }
                    Dictionary<string, PropertyInfo> dic = SAPReferentHelper.GetProperties(typeof(T));
                    if (dic.Count == 0)
                    {
                        throw new SAPException("不包含数据契约");
                    }
                    List<T> result = new List<T>();
                    T res = null;
                    Table tab = GetTableByName(attr.StructorName.ToUpper());     
                    for (int i = 1; i <= tab.RowCount; i++)
                    {
                        res = new T();
                        foreach (string s in dic.Keys)
                        {
                            object value = tab.get_Cell(i, s.ToUpper());
                            if (value != null)
                                dic[s].SetValue(res, ConvertObject(value, dic[s].PropertyType), null);
                        }
                        result.Add(res);
                    }
                    return result;
                }
                catch (Exception ex)
                {
                    throw new SAPException(ex.Message);
                }
            }
            /// <summary>
            /// 将tables转换成dataset
            /// </summary>
            /// <returns></returns>
            [Obsolete("最好别用,由于RFC类型转化为.net类型还没测试",false)]
            public DataSet GetDataSet()
            {
                try
                {
                    DataSet ds = new DataSet();
                    for (int i = 1; i <= Count; i++)
                    {
                        Table tab = GetTableByIndex(i);
                        ds.Tables.Add(GetDataTableByTab(tab));
                    }
                    return ds;
                }
                catch (Exception ex)
                {
                    throw new SAPException(ex.Message);
                }
            }        #endregion        #region private methods
            /// <summary>
            /// 将内表转化为datatable
            /// </summary>
            /// <param name="tab"></param>
            /// <returns></returns>
            private DataTable GetDataTableByTab(Table tab)
            {
                DataTable dt = new DataTable(tab.Name);
                if (tab.ColumnCount > 0)
                {
                    Columns cols = GetTableColumns(tab);
                    for (int i = 1; i <= cols.Count; i++)
                    {
                        Column col = GetColumnByIndex(cols, i);
                        DataColumn dc = dt.Columns.Add(col.Name, DoNetType.GetTypeFromRFC(col.Type));
                    }
                    for (int j = 1; j <= tab.RowCount; j++)
                    {
                        DataRow dr = dt.NewRow();
                        foreach (DataColumn dc in dt.Columns)
                        {
                            object value = tab.get_Cell(j, dc.ColumnName);
                            if (null != value)
                                dr[dc.ColumnName] = ConvertObject(value, dc.DataType);
                        }
                        dt.Rows.Add(dr);
                    }
                }
                return dt;
            }
            /// <summary>
            /// 获取table的所有列
            /// </summary>
            /// <param name="tab"></param>
            /// <returns></returns>
            private Columns GetTableColumns(Table tab)
            {
                return (Columns)tab.Columns;
            }
            /// <summary>
            /// 获取列
            /// </summary>
            /// <param name="cols"></param>
            /// <param name="index"></param>
            /// <returns></returns>
            private Column GetColumnByIndex(Columns cols,int index)
            {
                return (Column)cols.get_Item(index);
            }        /// <summary>
            /// 根据内表名获取table
            /// </summary>
            /// <param name="tablename"></param>
            /// <returns></returns>
            private Table GetTableByName(string tablename)
            {
                return (Table)_saptables.get_Item(tablename);
            }
            /// <summary>
            /// 根据内表index获取内表
            /// </summary>
            /// <param name="index"></param>
            /// <returns></returns>
            private Table GetTableByIndex(int index)
            {
                return (Table)_saptables.get_Item(index);
            }
            /// <summary>
            /// value不能为null 已经排除null情况
            /// 根据TYPE自动实现类型转换
            /// </summary>
            /// <param name="value"></param>
            /// <param name="t"></param>
            /// <returns></returns>
            private object ConvertObject(object value, Type t)
            {
                if (value.GetType() == t)
                {
                    return value;
                }
                if (!t.IsGenericType)
                {
                    return Convert.ChangeType(value, t);
                }
                else
                {
                    Type generictype = t.GetGenericTypeDefinition();
                    if (generictype == typeof(Nullable<>))
                    {
                        return Convert.ChangeType(value, Nullable.GetUnderlyingType(t));
                    }
                    throw new SAPException("该泛型不能被转换");
                }
            }        #endregion
        }
    }
      

  5.   


    using System;
    using System.Collections.Generic;
    using System.Text;
    using SAPLogonCtrl;
    using SAPFunctionsOCX;namespace ArtLee.ChintSAP.SAPClient
    {
        public class SAPFunction : IDisposable
        {
            #region public constructors        public SAPFunction(Connection conn, string rfc_name, SAPParameterCollection pars)
            {
                _connection = conn;
                _functionname = rfc_name;
                _sapparameters = pars;
            }        #endregion        #region private fields        private Connection _connection;        private string _functionname;        private SAPFunctionsClass _funcs;        private IFunction _curfunction;        private SAPParameterCollection _sapparameters;        private SAPTables _tables;        #endregion        #region public properties
            /// <summary>
            /// 获取和设置函数名
            /// </summary>
            public string FunctionName
            {
                get { return _functionname; }
            }        public SAPTables SAPTables
            {
                get { return _tables; }
            }        #endregion        #region public methods        public void Call()
            {
                try
                {
                    _curfunction = GetSAPFunction(_functionname);
                    InitRFCImprotParameters();
                    _curfunction.Call();
      
                    string strException = _curfunction.Exception;
                    if (!string.IsNullOrEmpty(strException))
                        throw new SAPException("RFC函数内部错误:" + strException);                SetRFCExportParmeters();
                    _tables = new SAPTables((SAPTableFactoryCtrl.Tables)(_curfunction.Tables));
                }
                catch (Exception ex)
                {
                    throw new SAPException(ex.Message);
                }
            }        #endregion        #region private methods        private void InitRFCImprotParameters()
            {
                try
                {
                    if (_sapparameters != null)
                    {
                        _sapparameters.ForEach(e =>
                        {
                            if (e.Option == ParameterOptions.Export)
                                ((IParameter)_curfunction.get_Exports(e.Name.ToUpper())).Value = e.Value;
                        });
                    }
                }
                catch (Exception)
                {
                    throw new SAPException("RFC函数import参数未找到");
                }
            }        private void SetRFCExportParmeters()
            {
                try
                {
                    if (_sapparameters != null)
                    {
                        _sapparameters.ForEach(e =>
                            {
                                if (e.Option == ParameterOptions.Import)
                                    e.Value = ((IParameter)_curfunction.get_Imports(e.Name.ToUpper())).Value;
                            });
                    }
                }
                catch (Exception)
                {
                    throw new SAPException("RFC函数export参数未找到");
                }
            }        /// <summary>
            /// 根据RFC函数名创建函数
            /// </summary>
            /// <param name="RFCfunctionName"></param>
            /// <returns></returns>
            private IFunction GetSAPFunction(string RFCfunctionName)
            {
                _funcs = new SAPFunctionsClass();
                _funcs.Connection = _connection;
                IFunction ifunc = (IFunction)_funcs.Add(RFCfunctionName.ToUpper());
                return ifunc;
            }        private void ReleaseCom(object obj)
            {
                if (null != obj)
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
            }        #endregion        #region IDisposable 成员        public void Dispose()
            {
                ReleaseCom(_connection);
                ReleaseCom(_funcs);
                ReleaseCom(_curfunction);
            }        #endregion
        }
    }
      

  6.   

    搞定了,原来是sap dll文件有升级,郁闷了我几天。
      

  7.   

    楼主,你好。我也遇到了这个问题,请问你是怎么解决的,很急谢谢。具体怎么做的SAP的DLL升级?