示例#1
0
        // 若是
        public T TryGetValue <T>(string key, T defaultValue, bool writeBackFlag)
        {
            T      result = defaultValue;
            object value  = this[key];

            if (value != null)
            {
                try
                {
                    object convertedObj = ConvertUtil.Convert(value, typeof(T));
                    if (convertedObj != null)
                    {
                        result = (T)convertedObj;
                    }
                }
                catch (Exception)
                {
                    // do nothing here.
                }
            }
            else
            {
                if (writeBackFlag)
                {
                    this[key] = defaultValue;
                }
            }

            return(result);
        }
示例#2
0
        public virtual T Excute <T>()
        {
            T result = default(T);

            try
            {
                dbCommand = owner.CreateCommand();

                BuildCommand();

                Type type = typeof(T);
                if (commandBehavior == BeeDbCommandBehavior.NoQuery)
                {
                    object obj = dbCommand.ExecuteScalar();

                    if (obj != null)
                    {
                        if (type == typeof(int) || type == typeof(long))
                        {
                            result = (T)ConvertUtil.Convert(obj, type);
                        }
                        else
                        {
                            throw new DataException("Not support the return type :" + type.Name);
                        }
                    }
                }
                else
                {
                    if (type == typeof(DataTable))
                    {
                        result = (T)ExecuteDataTable();
                    }
                    else if (type == typeof(DataSet))
                    {
                        result = (T)ExecuteDataSet();
                    }
                    else
                    {
                        throw new DataException("Not support the return type :" + type.Name);
                    }
                }

                object retVal = FinishCommand();
                if (retVal != null)
                {
                    result = (T)ConvertUtil.Convert(retVal, type);
                }

                //Logger.Log(LogLevel.Core, this.ToSqlString());
            }
            catch (DbException dbException)
            {
                //if (dbCommand != null)
                //{
                //    Logger.Debug(string.Format("DbCmdText:{0}\r\nconnection:{1}", dbCommand.CommandText, owner.DbDriver), dbException);
                //}
                throw new DataException(string.Format("{0}\r\n{1}", dbException.Message, this.ToString()), dbException);
            }
            catch (Exception ex)
            {
                //if (dbCommand != null)
                //{
                //    Logger.Debug(string.Format("DbCmdText:{0}", dbCommand.CommandText), ex);
                //}
                throw new DataException(string.Format("{0}\r\n{1}", ex.Message, this.ToString()), ex);
            }
            finally
            {
                if (dbCommand != null)
                {
                    dbCommand.Parameters.Clear();
                    dbCommand.Dispose();
                }

                // 若是默认session, 则关闭数据库连接
                if (owner.IsDefaultSession)
                {
                    owner.CloseConnection();
                }
            }

            return(result);
        }
 public static IEnumerable <TResult> Cast <T, TResult>(this IEnumerable <T> self)
 {
     return(self.Select(item => ConvertUtil.Convert <T, TResult>(item)));
 }