在.NET 4环境下,如何处理包含DbNull的DataReader数据读取?

2026-05-08 12:253阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计396个文字,预计阅读时间需要2分钟。

在.NET 4环境下,如何处理包含DbNull的DataReader数据读取?

在框架4中,有一个字段扩展方法允许从一个datareader接收空值,而无需经过首次测试过程。若该值非null,则...(更多信息请参考MSDN)但我不知道如何在代码中实现这一点。

我听说框架4中有一个字段扩展方法允许一个接收来自datareader的空值,而不必经过第一次测试的过程,如果不是null那么……等等.这里有关于扩展方法的信息( MSDN) ),但我不知道如何在代码中使用它(相对较新的.net和以前从未使用过的扩展方法).如果有人能举一个例子,我将不胜感激.

这是我试图实现的,但是当在任一列中返回dbnull时它会返回错误.

Reader.Read() Dim Val As Nullable(Of Double) = Reader.GetDecimal(0) Dim Vol As Nullable(Of Long) = Reader.GetInt32(1) 这些扩展方法涉及DataRow – 即DataTable ……而不是IDataReader(etc).
你可以用条件来做你想要的事情 – 虽然在VB中使用IIf,或者在C#中:

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

您当然可以将它们作为实用程序方法包装起来,也许作为您自己的IDataReader上的自定义扩展方法:

在.NET 4环境下,如何处理包含DbNull的DataReader数据读取?

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 }

(抱歉使用c#作为示例 – 但你可以阅读c#比写出准确的vb.net更好)

标签:data

本文共计396个文字,预计阅读时间需要2分钟。

在.NET 4环境下,如何处理包含DbNull的DataReader数据读取?

在框架4中,有一个字段扩展方法允许从一个datareader接收空值,而无需经过首次测试过程。若该值非null,则...(更多信息请参考MSDN)但我不知道如何在代码中实现这一点。

我听说框架4中有一个字段扩展方法允许一个接收来自datareader的空值,而不必经过第一次测试的过程,如果不是null那么……等等.这里有关于扩展方法的信息( MSDN) ),但我不知道如何在代码中使用它(相对较新的.net和以前从未使用过的扩展方法).如果有人能举一个例子,我将不胜感激.

这是我试图实现的,但是当在任一列中返回dbnull时它会返回错误.

Reader.Read() Dim Val As Nullable(Of Double) = Reader.GetDecimal(0) Dim Vol As Nullable(Of Long) = Reader.GetInt32(1) 这些扩展方法涉及DataRow – 即DataTable ……而不是IDataReader(etc).
你可以用条件来做你想要的事情 – 虽然在VB中使用IIf,或者在C#中:

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

您当然可以将它们作为实用程序方法包装起来,也许作为您自己的IDataReader上的自定义扩展方法:

在.NET 4环境下,如何处理包含DbNull的DataReader数据读取?

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 }

(抱歉使用c#作为示例 – 但你可以阅读c#比写出准确的vb.net更好)

标签:data