使用的DataReader在.NET4的DBNull值 [英] using datareader with dbnull values in .net4

查看:386
本文介绍了使用的DataReader在.NET4的DBNull值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说有在框架4,允许一个从DataReader接收空值的字段扩展方法,而不必经过第一次的测试过程空,则如果不是......等等有关于扩展方法的信息在这里( MSDN ),但我不知道如何使用它在code(是比较新的.NET和之前从未使用过的扩展方法)。会美联社preciate如果任何人都可以举个例子。

这就是我想实现,但它返回时,为DBNull在任一列则返回一个错误。

  Reader.Read()
昏暗瓦尔为可为空(双)= Reader.GetDecimal(0)
昏暗卷为可为空(长)= Reader.GetInt32(1)
 

解决方案

这些扩展方法涉及到的DataRow - 即数据表 ... 没有的IDataReader (等)。 你可以做你想要的这里条件,但 - IIF 在VB或C#:

 翻番? VAL = reader.IsDBNull(指数)? (双倍?)空:reader.GetDouble(指数);
长?体积= reader.IsDBNull(指数)? (长?)空:reader.GetInt64(指数);
 

您可以当然,这些包装起来的实用方法,也许是因为对自己的自定义扩展方法的IDataReader

 公共静态类DataReaderExtensions
{
    公共静态诠释? ReadNullableInt32(这IDataReader的读者,INT指数)
    {
        返回reader.IsDBNull(指数)? (INT?)空:reader.GetInt32(指数);
    }
    公共静态长? ReadNullableInt64(这IDataReader的读者,INT指数)
    {
        返回reader.IsDBNull(指数)? (长?)空:reader.GetInt64(指数);
    }
    公共静态双? ReadNullableDouble(这IDataReader的读者,INT指数)
    {
        返回reader.IsDBNull(指数)? (双倍?)空:reader.GetDouble(指数);
    }
    公共静态字符串ReadNullableString(这IDataReader的读者,INT指数)
    {
        返回reader.IsDBNull(指数)?空:reader.GetString(指数);
    }
    // 等等
}
 

(对不起,使用C#的例子 - 但是你也许可以阅读C#的比我可以写的准确的vb.net)

I heard there is a field extension method in framework 4 that permits one to receive null values from a datareader, without having to go through the process of first testing if not null then ... etc. There's information about the extension method here (MSDN), but i don't know how to use it in code (am relatively new to .net and never used extension methods before). Would appreciate if anyone can give an example.

This is what i tried to implement, but it returns an error when a dbnull is returned in either one of the columns.

Reader.Read()
Dim Val As Nullable(Of Double) = Reader.GetDecimal(0)
Dim Vol As Nullable(Of Long) = Reader.GetInt32(1)

解决方案

Those extension methods relate to DataRow - i.e. DataTable... not IDataReader (etc). You can do what you want here with a conditional, though - IIf in VB, or in C#:

double? val = reader.IsDBNull(index) ? (double?) null : reader.GetDouble(index);
long? vol = reader.IsDBNull(index) ? (long?)null : reader.GetInt64(index);

You could of course wrap those up as utility methods, perhaps as your own custom extension methods on IDataReader:

public static class DataReaderExtensions
{
    public static int? ReadNullableInt32(this IDataReader reader, int index)
    {
        return reader.IsDBNull(index) ? (int?)null : reader.GetInt32(index);
    }
    public static long? ReadNullableInt64(this IDataReader reader, int index)
    {
        return reader.IsDBNull(index) ? (long?)null : reader.GetInt64(index);
    }
    public static double? ReadNullableDouble(this IDataReader reader, int index)
    {
        return reader.IsDBNull(index) ? (double?)null : reader.GetDouble(index);
    }
    public static string ReadNullableString(this IDataReader reader, int index)
    {
        return reader.IsDBNull(index) ? null : reader.GetString(index);
    }
    // etc
}

(sorry for using c# for the examples - but you can probably read c# better than I can write accurate vb.net)

这篇关于使用的DataReader在.NET4的DBNull值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆