DataReader的:无效尝试读取时没有数据present [英] DataReader : Invalid attempt to read when no data is present

查看:177
本文介绍了DataReader的:无效尝试读取时没有数据present的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经遇到了一些有趣的现象。我有以下的code从DB(ASP.net)填充字段的值:

I have faced some interesting situation. I have the following code to populate value of field from DB (ASP.net):

SqlConnection connect =
            new SqlConnection(
                @"conn-string"); 
SqlCommand toDo = new SqlCommand(InfoQuery, connect);
        toDo.CommandTimeout = 6000;
        connect.Open();
        using (SqlDataReader reader = toDo.ExecuteReader())
        {
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    ///......retrieving some fields in the same way as below
                    foo = reader.SafeGetString(7);
                    int temp = reader.SafeGetInt32(8);
                    ///......retrieving again
                }
        }
        connect.close()

的连接被建立,所有PARAMS是正确的。在SQL Server Management Studio中使用 TODO 命令与完善相关的查询。在程序运行的各个领域的温度时值(不包括临时)被检索和设置。但看完温度值时,出现以下异常:

The connection is established, all params are correct. In SQL Server Management Studio the query associated with toDo command works just perfect. In program when running every field to the temp value (not including temp) is retrieved and set. But when reading temp value, i get the following exception:

无效的尝试。

这是我的扩展方法:

public static class ExtentionMethods
{
    public static string SafeGetString(this SqlDataReader reader, int colIndex)
    {
        if (!reader.IsDBNull(colIndex))
            return reader.GetString(colIndex);
        return "NULL VALUE";
    }

    public static int SafeGetInt32(this SqlDataReader reader, int colIndex)
    {
        if (!reader.IsDBNull(colIndex))
            return reader.GetInt32(colIndex);
        return -1;
    }

    public static DateTime SafeGetDateTime(this SqlDataReader reader, int colIndex)
    {
        if (!reader.IsDBNull(colIndex))
        {
            try
            {
            }
            catch
            {
                return new DateTime(1800, 1, 1);
            }
        }
        return new DateTime(1800, 1, 1);
    }
}

查询:

SELECT TOP 1000 [ID0]
        ,[Id1]
        ,[Id2]
        ,Id1+Id2+'0' AS BC
        ,[Id3]
        ,[Id4]
        ,[Id5]
        ,CAST([Date] AS nvarchar(max))
        ,[int]
        ,[Id7]
        ,[Id8]
        ,IsNull(foo,'dsf')+' '+IsNull(bar,'dsf')+', '
        +IsNull(fgm,'fggf')+', fgfggf '+IsNull(gfgf,'gfgf')+
        ','+dfsdfsdsf+', '+dsffddf AS dsadsa
        ,[fg]
        ,[fds]
        FROM tbl
        inner join tbl1 on tbl1.ID=ID1
        inner join tbl2 on tbl2.ID=ID2
        WHERE Id4=12

这个问题可能是什么?

What the problem could be?

推荐答案

我不认为你需要这一切的扩展方法,你应该按名称访问列:

I don't think you need all this extensions methods, and you should access columns by name :

while (reader.Read())
{
    int intCol = reader["MyIntColumn"] as int? ?? -1;
    string stringCol = reader["MyStringColumn"] as string ?? "NULL VALUE";
    DateTime dateCol = reader["MyDateColumn"] as DateTime? ?? new DateTime(1800, 1, 1);
}

INT?的DateTime?将允许空值和 ?? 如果列空会影响到默认值。

int? or DateTime? will allow nulls and ?? will affect a default value if the column is null.

这篇关于DataReader的:无效尝试读取时没有数据present的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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