日期:2014-05-19  浏览次数:20969 次

如何用SqlDataReader读出多行数据?
代码是这样的,已经定义好连接了.
String   sql   =   "select     所得税   from   企业信息,企业所得税   where   企业信息.企业名称=企业所得税.企业名称     and   企业信息.所在地区= '北市区 '; ";
                        this.sqlCommand1.Connection   =   sqlConnection1;
                        this.sqlCommand1.CommandText   =   sql;

                        this.sqlConnection1.Open();

                        SqlDataReader   myReader;
                        myReader   =   sqlCommand1.ExecuteReader();

下面该怎么写?怎么样一个一个的读出?我试了用while循环,
int   i=1;
                        while   (myReader.Read()|i <10)
                        {
                                textBox1.Text   +=   myReader[i];
                                i++;
                        }

可是总是提示错误说“索引超出了数组界限。”错误在哪里呢?请帮帮忙!谢谢了!

------解决方案--------------------
private void button1_Click(object sender, EventArgs e)
{
// 连接
SqlConnection cn = new SqlConnection( "server=.;database=northwind;uid=sa;pwd=0 ");
cn.Open();

// 查询
SqlCommand cmd = new SqlCommand( "select * from employees ", cn);

// 读取
SqlDataReader sda = cmd.ExecuteReader();

while (sda.Read())
{
listBox1.Items.Add(sda.GetString(2)); // 第2列数据
}

// 释放
sda.Close();
cn.Close();
}
------解决方案--------------------
SqlDataReader myReader;
myReader = sqlCommand1.ExecuteReader();
while (myReader.Read())
{
for(int i=0;i <myReader.FieldCount;i++)
{
listBox1.Items.Add(myReader[i].ToString());
}

}
myReader.Close();
上面的忘了tostring