C#中实用的数据访问扩展类

using System;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;

namespace ICCS
{
    public static class DataAccessExtension
    {
        /// <summary>
        /// 将DataReader中指定列的数据类型转为特定类型
        /// </summary>
        /// <typeparam name="T">泛型类型</typeparam>
        /// <param name="columnName">列名称</param>
        public static T Get<T>(this IDataReader reader, string columnName)
        {
            return Get<T>(reader, reader.GetOrdinal(columnName));
        }
        /// <summary>
        /// 将DataReader中指定列的数据类型转为特定类型
        /// </summary>
        /// <typeparam name="T">泛型类型</typeparam>
        /// <param name="index">列索引</param>
        public static T Get<T>(this IDataReader reader, int index)
        {
            return Get<T>(reader, index, false);
        }
        /// <summary>
        /// 将DataReader中指定列的数据类型转为特定类型
        /// </summary>
        /// <typeparam name="T">泛型类型</typeparam>
        /// <param name="columnName">列名称</param>
        /// <param name="isCustomerType">指定列是否为自定义类型</param>
        public static T Get<T>(this IDataReader reader, string columnName, bool isCustomerType)
        {
            return Get<T>(reader, reader.GetOrdinal(columnName), isCustomerType);
        }
        /// <summary>
        /// 将DataReader中指定列的数据类型转为特定类型
        /// </summary>
        /// <typeparam name="T">泛型类型</typeparam>
        /// <param name="index">列索引</param>
        /// <param name="isCustomerType">指定列是否为自定义类型</param>
        public static T Get<T>(this IDataReader reader, int index, bool isCustomerType)
        {
            object attemptedValue = reader.IsDBNull(index) ? null : reader[index];
            var type = typeof(T);
            try
            {
                if (attemptedValue != null)
                {
                    if (isCustomerType)
                    {
                        var converter = TypeDescriptor.GetConverter(type);
                        if (converter != null && converter.IsValid(attemptedValue))
                        {
                            return (T)converter.ConvertFrom(attemptedValue);
                        }
                    }
                    else
                    {
                        if (typeof(T).IsEnum && Enum.IsDefined(typeof(T), attemptedValue))
                        {
                            return (T)Enum.ToObject(typeof(T), attemptedValue);
                        }
                        return (T)Convert.ChangeType(attemptedValue, type);
                    }
                }
                if (isCustomerType)
                {
                    return (T)Activator.CreateInstance(type);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return default(T);
        }
        /// <summary>
        /// 构造存储过程输出参数
        /// </summary>
        /// <param name="name">参数名</param>
        /// <returns>存储过程输出参数</returns>
        public static SqlParameter Out(this SqlDbType dbType, string name)
        {
            return new SqlParameter(name, dbType) { Direction = System.Data.ParameterDirection.Output };
        }
        /// <summary>
        /// 构造存储过程输出参数
        /// </summary>
        /// <param name="name">参数名</param>
        /// <param name="size">参数类型大小</param>
        /// <returns>存储过程输出参数</returns>
        public static SqlParameter Out(this SqlDbType dbType, string name, int size)
        {
            return new SqlParameter(name, dbType, size) { Direction = System.Data.ParameterDirection.Output };
        }
        /// <summary>
        /// 构造存储过程传入参数
        /// </summary>
        /// <param name="name">参数名</param>
        /// <param name="value">参数值</param>
        /// <returns>存储过程传入参数</returns>
        public static SqlParameter In(this SqlDbType dbType, string name, object value, bool isNullable = true)
        {
            if (isNullable && (value == null || value.ToString().Length == 0))
            {
                value = DBNull.Value;
            }
            return new SqlParameter(name, dbType) { Direction = System.Data.ParameterDirection.Input, Value = value, IsNullable = isNullable };
        }
        /// <summary>
        /// 构造存储过程传入参数
        /// </summary>
        /// <param name="name">参数名</param>
        /// <param name="size">参数类型大小</param>
        /// <param name="value">参数值</param>
        /// <returns>存储过程传入参数</returns>
        public static SqlParameter In(this SqlDbType dbType, string name, int size, object value, bool isNullable = true)
        {
            if (isNullable && (value == null || value.ToString().Length == 0))
            {
                value = DBNull.Value;
            }
            return new SqlParameter(name, dbType, size) { Direction = System.Data.ParameterDirection.Input, Value = value, IsNullable = isNullable };
        }
    }
}

posted @ 2016-12-05 16:40  CH|heyi  阅读(126)  评论(0编辑  收藏  举报