数组代码中的错误 [英] error in code of array

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

问题描述

错误:-索引超出了数组的范围
我的代码是:-

Error:-Index was outside the bounds of the array
my code is:-

SqlDataReader reader = cmd4.ExecuteReader();

  while (reader.Read())
  {
      Label6.Text = Convert.ToString(reader[0]);
      Label8.Text = Convert.ToString(reader[1]); //it gives error on this line.
      Label10.Text = Convert.ToString(reader[2]);
  }


所以请尝试解决它...


so plz try to solve it...

推荐答案

如此处

一种可能是Reader 返回的Row 可能只有 one column.

请注意Label6.Text and check the contents of the reader,以查看其是否具有所需的列数.否则,请检查cmd4 以确保返回所需的列数.进一步,而不是使用Convert.ToString而不是GetString 方法,此处说明
As explained here http://msdn.microsoft.com/en-us/library/haa3afyz(v=vs.90).aspx[^], "You use the Read method of the DataReader object to obtain a row from the results of the query. You can access each column of the returned row by passing the name or ordinal reference of the column to the DataReader. However, for best performance, the DataReader provides a series of methods that allow you to access column values in their native data types (GetDateTime, GetDouble, GetGuid, GetInt32, and so on). For a list of typed accessor methods for data provider-specific DataReaders, see OleDbDataReader and SqlDataReader. Using the typed accessor methods, assuming the underlying data type is known, reduces the amount of type conversion required when retrieving the column value."

One possibility could be that the Row returned by the Reader may be having only one column.

Please but a bread point at Label6.Text and check the contents of the reader to see whether it has required number of columns. Otherwise, please check the cmd4 for ensuring to return the required number of columns. Further instead of using Convert.ToString the GetString method explained here http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getstring(v=vs.90)[^] can be used as suggested in the above reference.


使用DataReader的方式不正确.

更改为:
The way you use DataReader is not correct.

Change to:
SqlDataReader reader = command.ExecuteReader();
        int i=0;
        if (reader.HasRows)
        {
            while (reader.Read())
            {
                Console.WriteLine(reader[i].ToString());
                ++i;
            }
        }
        else
        {
            Console.WriteLine("No rows found.");
        }
        reader.Close();


看看这些:
使用DataReader(ADO.NET)检索数据 [ ^ ]
DataReader类 [


Look at these:
Retrieving Data Using a DataReader (ADO.NET)[^]
DataReader Class[^]




检查您的命令是否返回了多少列.仅当所需资源不可用时,它才可能发生.

同时发布您的命令代码以准确识别.

谢谢
-Amit
Hi,

Check if your command have how many column returned. it may happen only if required resource is not available.

Also post your command code to identify accurately.

thanks
-Amit


这篇关于数组代码中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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