コード例 #1
0
        /// <summary>
        /// 从数据库中获取对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T GetSetting <T>(string repUri) where T : class
        {
            try
            {
                SystemParameterBLL             bll    = new SystemParameterBLL(repUri);
                SysparameterInfo               para   = null;
                QueryResult <SysparameterInfo> result = null;

                Type t = typeof(T);
                result = bll.GetByID(t.Name);

                if (result.Result == ResultCode.Successful)
                {
                    para = result.QueryObject;
                    string value = para.Value;
                    if (!string.IsNullOrEmpty(value))
                    {
                        byte[] data = Encoding.UTF8.GetBytes(value);
                        using (MemoryStream stream = new MemoryStream(data))
                        {
                            stream.Position = 0;
                            DataContractSerializer ser = new DataContractSerializer(t);
                            return(ser.ReadObject(stream) as T);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionPolicy.HandleException(ex);
            }
            return(null);
        }
コード例 #2
0
        /// <summary>
        ///保存到数据库
        /// </summary>
        /// <param name="info"></param>
        public static CommandResult SaveSetting <T>(T info, string repUri) where T : class
        {
            try
            {
                SystemParameterBLL bll = new SystemParameterBLL(repUri);
                Type t = typeof(T);

                DataContractSerializer ser = new DataContractSerializer(t);
                using (MemoryStream stream = new MemoryStream())
                {
                    ser.WriteObject(stream, info);
                    stream.Position = 0;
                    byte[] data = new byte[stream.Length];
                    stream.Read(data, 0, (int)stream.Length);
                    string           value = Encoding.UTF8.GetString(data);
                    SysparameterInfo para  = new SysparameterInfo(t.Name, value, string.Empty);
                    return(bll.Save(para));
                }
            }
            catch (Exception ex)
            {
                ExceptionPolicy.HandleException(ex);
                return(new CommandResult(ResultCode.Fail, ex.Message));
            }
        }