SqlDataReader超出范围错误

编程入门 行业动态 更新时间:2024-10-08 06:24:23
本文介绍了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超出范围错误

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

发布评论

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

>www.elefans.com

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