SqlDataReader超出范围错误 [英] Out of range error on SqlDataReader

查看:62
本文介绍了SqlDataReader超出范围错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SqlDataReader 并在尝试读取列时得到此异常...

I'm using a SqlDataReader and get this exception when trying to read a column...

System.IndexOutOfRangeException:记录

这是代码...

SqlCommand select = new SqlCommand("SELECT RTRIM(LTRIM(PART_NO)) AS PART_NO, record AS CUSTOMER_NO FROM [RMAData].[dbo].[IMPORTING_ORDER_EDI] WHERE sessionID = '" + Session.SessionID + "'", connection);
SqlDataReader reader = select.ExecuteReader();

if (reader.HasRows)
{
   while (reader.Read())
   {
       lblWebMasterMessage.Text += "record " + reader["record"].ToString() + "<br />";
...

如果我将 lblWebMasterMessage.Text 更改为以下内容,则效果很好...

If I change the lblWebMasterMessage.Text to the following it works just fine...

lblWebMasterMessage.Text += "record " + reader["PART_NO"].ToString() + "<br />";

SQL Server表中record和 PART_NO 之间的区别是'record'是主键,而 int PART_NO varchar(100).

The difference between record and PART_NO in the SQL Server table is that 'record' is a primary key and an int, PART_NO is a varchar(100).

麻烦的是,我需要记录"来标识记录以便以后对其进行更新...

The trouble is, I need the 'record' to identify the record to update it later...

我真的不明白为什么它可以返回一个字段而不返回另一个字段?

I really can't see why it can return one field and not the other?

推荐答案

这是因为您没有名为"record"的字段,因此将其别名为"CUSTOMER_NO",因此将代码更改为:

That's because you have no field named "record", you aliased it to "CUSTOMER_NO" so change the code to:

lblWebMasterMessage.Text += "record " + reader["CUSTOMER_NO"].ToString() + "<br />";

也就是说,您也可以使用index而不是name来读取第二列:

That said, you can also use index instead of name so to read the second column:

lblWebMasterMessage.Text += "record " + reader[1] + "<br />";

这篇关于SqlDataReader超出范围错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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