如何解决对位值的IndexOutOfRangeException?

编程入门 行业动态 更新时间:2024-10-26 18:19:37
本文介绍了如何解决对位值的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)

推荐答案

的文档说,在 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?

本文发布于:2023-11-02 08:40:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1551916.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何解决   IndexOutOfRangeException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!