如何解决对位值的IndexOutOfRangeException? [英] How to resolve an IndexOutOfRangeException on bit value?

查看:1205
本文介绍了如何解决对位值的IndexOutOfRangeException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在读回从SQL表中位值,和铸造作为一个布尔值映射到一个布尔模型值。

I'm reading back a bit value from an SQL table, and casting as a boolean value to map to a boolean model value.

但是,当我读回 IsPDLChecked 位字段并初始化为假,我得到一个索引超出范围异常

But when I read back the IsPDLChecked bit field and initialise as false, I get an index out of range exception.

我抬头异常的定义,我不知道为什么值超出范围,由于它是初始化一个假值的,即 0 。因此,这不是一个消极的范围值。

I looked up the definition of the exception and I'm not sure why the value is out of range due to it being init with a false value, ie, 0. So it's not a negative range value.

问:

为什么我收到一个 IndexOutOfRange例外虽然被读回位值转换为BOOL和初始化为假?

Why am I getting an IndexOutOfRange exception although the bit value being read back is cast to bool and init to false?

code:

示范 -

public partial class EmailContact
{
    public int ContactID { get; set; }
    public bool IsPDLChecked { get; set; }
    public string ContactDisplayName { get; set; }
    public string ContactEmailAddress { get; set; }   

}

数据读取片段 -

Data reader snippet -

                        while (dataReader.Read())
                        {
                            statusList.Add(new EmailContact(dataReader["IsPDLChecked"] as bool? ?? false,dataReader["ContactDisplayName"].ToString(), dataReader["ContactEmailAddress"].ToString()));
                        }

错误详细信息:

System.IndexOutOfRangeException was caught
  HResult=-2146233080
  Message=IsPDLChecked
  Source=System.Data
  StackTrace:
       at System.Data.ProviderBase.FieldNameLookup.GetOrdinal(String fieldName)
       at System.Data.SqlClient.SqlDataReader.GetOrdinal(String name)
       at System.Data.SqlClient.SqlDataReader.get_Item(String name)

DB模式:(我也注意到了CHARACTER_MAX_LENGTHcollumn被设置为null这个):

DB值:(IsPDLChecked有一个值)

DB values: (IsPDLChecked has a value)

在这里输入的形象描述

推荐答案

的<一个href=\"https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getordinal(v=vs.110).aspx\"相对=nofollow>文档说,在 IndexOutOfRangeException 你要当指定的名称不是有效的列名被抛出。

The documentation says that the IndexOutOfRangeException you're getting is thrown when "the name specified is not a valid column name".

你应该做的第一件事是在C#中的比赛,以确保列名称与表中(或在查询,如果他们被重新定义):

The first thing you should do is to make sure the columns name in C# match those in the table (or in the query if they are redefined):

SELECT ContactDisplayName, ContactEmailAddress, IsPDLChecked FROM table

SELECT * FROM table

将返回的列名,就可以相同的,因为它们是在表中和读者要细,因为它是现在。

will return column names that will be the same as they are in the table and your reader should be fine as it is right now.

但是,如果您的查询是这样的:

But if your query looks like this:

SELECT ContractDisplayName as Name,
       ContactEmailAddress as Email,
       IsPDLChecked as Checked
  FROM table

您将需要在C#中读取结果是这样的:

you will need to read the result like this in C#:

dataReader["Name"]
dataReader["Email"]
dataReader["Checked"]

这篇关于如何解决对位值的IndexOutOfRangeException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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