Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在ado.net中,从SqlDataReader获取值的最佳实用程序函数是什么?_C#_Generics_Reflection_Ado.net - Fatal编程技术网

C# 在ado.net中,从SqlDataReader获取值的最佳实用程序函数是什么?

C# 在ado.net中,从SqlDataReader获取值的最佳实用程序函数是什么?,c#,generics,reflection,ado.net,C#,Generics,Reflection,Ado.net,从SqlDataReader获取值的最佳方法是什么 我编写了一个实用函数,可以从底层数据类型中按列名检索值,请任何人提出改进建议 P>我认为效率低下的是单独为每个类型提供实现,这会导致许多类型未处理。 对于我没有处理的类型,我的代码能否给出编译类型错误?我考虑过使用通用约束,但在我的情况下,它们并没有完全起作用 public static T GetValue<T>(SqlDataReader reader, string columnName,T defaultValue=def

SqlDataReader
获取值的最佳方法是什么

我编写了一个实用函数,可以从底层数据类型中按列名检索值,请任何人提出改进建议

<> P>我认为效率低下的是单独为每个类型提供实现,这会导致许多类型未处理。

对于我没有处理的类型,我的代码能否给出编译类型错误?我考虑过使用通用约束,但在我的情况下,它们并没有完全起作用

public static T GetValue<T>(SqlDataReader reader, string columnName,T defaultValue=default(T))
{
    int ordinal;

    // checking whether column name exist in reader result set
    try
    {
        ordinal = reader.GetOrdinal(columnName);
    }
    catch (IndexOutOfRangeException columnException)
    {
       var innerexception = new IndexOutOfRangeException(String.Format("Column name does not exist in specified SqlDataReader, {0}", columnException.Message), columnException);

       throw innerexception;
   }
   catch (Exception e)
   {
       throw e;
   }

   // Defining Types and their corresponding functions to be called.
   var typeDictionary = new Dictionary<Type, Func<T>>
            {
                {typeof(Int16), () => (T)Convert.ChangeType(reader.GetInt16(ordinal),typeof(T))},
                {typeof(Int32), () => (T)Convert.ChangeType(reader.GetInt32(ordinal),typeof(T))},
                {typeof(Int64), () => (T)Convert.ChangeType(reader.GetInt64(ordinal),typeof(T))},
                {typeof(String), () => (T)Convert.ChangeType(reader.GetString(ordinal),typeof(T))},
                {typeof(Boolean), () => (T)Convert.ChangeType(reader.GetBoolean(ordinal),typeof(T))},
                {typeof(Byte), () => (T)Convert.ChangeType(reader.GetByte(ordinal),typeof(T))},
                {typeof(DateTime), () => (T)Convert.ChangeType(reader.GetDateTime(ordinal),typeof(T))},
                {typeof(Char), () => (T)Convert.ChangeType(reader.GetChar(ordinal),typeof(T))},
                {typeof(Decimal), () => (T)Convert.ChangeType(reader.GetDecimal(ordinal),typeof(T))},
                {typeof(Double), () => (T)Convert.ChangeType(reader.GetDouble(ordinal),typeof(T))}
            };

   Type providedType = typeof (T);

   // Handling nullable types, If programmer wants to pass null value as default he should pass struct type with '?'.
   if (providedType.IsGenericType && 
       providedType.GetGenericTypeDefinition() == typeof (Nullable<>))
   {
       providedType = Nullable.GetUnderlyingType(providedType);
   }

   if (typeDictionary[providedType] == null)
   {
       throw new ArgumentException("Not Supported Type");
   }

   var resultedValue = reader.IsDBNull(ordinal) ? defaultValue : typeDictionary[typeof (T)]();

   return resultedValue;
}
publicstatict GetValue(SqlDataReader读取器,string columnName,T defaultValue=default(T))
{
整数序数;
//正在检查读卡器结果集中是否存在列名
尝试
{
序号=reader.GetOrdinal(columnName);
}
catch(IndexOutOfRangeException列异常)
{
var innerexception=new IndexOutOfRangeException(String.Format(“指定的SqlDataReader,{0}”、columnException.Message、columnException中不存在列名”);
抛出内部异常;
}
捕获(例外e)
{
投掷e;
}
//定义要调用的类型及其对应函数。
var typeDictionary=新字典
{
{typeof(Int16),()=>(T)Convert.ChangeType(reader.GetInt16(ordinal),typeof(T))},
{typeof(Int32),()=>(T)Convert.ChangeType(reader.GetInt32(ordinal),typeof(T))},
{typeof(Int64),()=>(T)Convert.ChangeType(reader.GetInt64(ordinal),typeof(T))},
{typeof(String),()=>(T)Convert.ChangeType(reader.GetString(ordinal),typeof(T))},
{typeof(Boolean),()=>(T)Convert.ChangeType(reader.GetBoolean(ordinal),typeof(T))},
{typeof(Byte),()=>(T)Convert.ChangeType(reader.GetByte(ordinal),typeof(T))},
{typeof(DateTime),()=>(T)Convert.ChangeType(reader.GetDateTime(ordinal),typeof(T))},
{typeof(Char),()=>(T)Convert.ChangeType(reader.GetChar(ordinal),typeof(T))},
{typeof(Decimal),()=>(T)Convert.ChangeType(reader.GetDecimal(ordinal),typeof(T))},
{typeof(Double),()=>(T)Convert.ChangeType(reader.GetDouble(ordinal),typeof(T))}
};
提供的类型类型=类型(T);
//处理可为null的类型时,若程序员希望传递null值作为默认值,则应传递带“?”的结构类型。
if(providedType.IsGenericType&&
providedType.GetGenericTypeDefinition()==typeof(可为空))
{
providedType=Nullable.GetUnderlineType(providedType);
}
if(类型字典[providedType]==null)
{
抛出新ArgumentException(“不支持的类型”);
}
var resultedValue=reader.IsDBNull(序数)?默认值:typeDictionary[typeof(T)]();
返回结果值;
}

为什么需要实用功能?为什么
SqlDataReader
类公开的功能/方法还不够?另外,捕获和释放虽然是一种流行的钓鱼形式,但对于异常来说是不明智的。我建议删除
catch(异常e)
block。@Daniel要从reader中获取列的值,我必须始终检查null并将其转换为相关类型,以使代码最小化。改进工作代码的建议包括:为什么需要实用函数?为什么
SqlDataReader
类公开的功能/方法还不够?另外,捕获和释放虽然是一种流行的钓鱼形式,但对于异常来说是不明智的。我建议删除
catch(异常e)
block。@Daniel要从reader中获取列的值,我必须始终检查null并将其转换为相关类型,以最小化代码。关于改进工作代码的建议,请参见