我正在使用 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超出范围错误
发布评论