Exemple #1
0
        /// <summary>
        /// 返回执行指定Sql语句后返回的结果
        /// </summary>
        /// <typeparam name="T">数据类型</typeparam>
        /// <param name="dbCommand">数据库命令</param>
        /// <param name="dataType">数据类型</param>
        /// <param name="connection">数据库连接</param>
        /// <param name="transaction">数据库事务</param>
        /// <returns>返回的结果</returns>
        public T ExecuteScalar <T>(DbCommand dbCommand, DataType dataType, DbConnection connection, DbTransaction transaction)
        {
            if (dbCommand == null)
            {
                throw new ArgumentNullException("dbCommand");
            }

            Object obj = this.ExecuteScalar(dbCommand, connection, transaction);

            return(DbConvert.IsDBNull(obj) ? default(T) : (T)DbConvert.ToValue(obj, dataType));
        }
        /// <summary>
        /// 将数据库字段按数据类型转换
        /// </summary>
        /// <param name="obj">原始数据库字段</param>
        /// <param name="dataType">数据类型</param>
        /// <returns>转换后的内容</returns>
        public static Object ToValue(Object obj, DataType dataType)
        {
            switch (dataType)
            {
            case DataType.Binary:
                return((Byte[])obj);

            case DataType.Boolean:
                return(DbConvert.ToBoolean(obj));

            case DataType.Byte:
                return(DbConvert.ToByte(obj));

            case DataType.SByte:
                return(DbConvert.ToSByte(obj));

            case DataType.Int16:
                return(DbConvert.ToInt16(obj));

            case DataType.Int32:
                return(DbConvert.ToInt32(obj));

            case DataType.Int64:
                return(DbConvert.ToInt64(obj));

            case DataType.UInt16:
                return(DbConvert.ToUInt16(obj));

            case DataType.UInt32:
                return(DbConvert.ToUInt32(obj));

            case DataType.UInt64:
                return(DbConvert.ToUInt64(obj));

            case DataType.Single:
                return(DbConvert.ToSingle(obj));

            case DataType.Double:
                return(DbConvert.ToDouble(obj));

            case DataType.Currency:
            case DataType.Decimal:
            case DataType.VarNumeric:
                return(DbConvert.ToDecimal(obj));

            case DataType.Object:
                return(obj);

            case DataType.Date:
            case DataType.DateTime:
            case DataType.DateTime2:
            case DataType.Time:
                return(DbConvert.ToDateTime(obj));

            case DataType.DateTimeOffset:
                return(DbConvert.ToDateTimeOffset(obj));

            case DataType.Guid:
                return(DbConvert.ToGuid(obj));

            case DataType.AnsiString:
            case DataType.AnsiStringFixedLength:
            case DataType.String:
            case DataType.StringFixedLength:
            case DataType.Xml:
                return(DbConvert.ToString(obj));

            default:
                return(obj);
            }
        }