sql datareader问题 [英] sql datareader problem

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

问题描述

这是我的代码:

this is my code:

using (conn)
{
    conn.Open();
    SqlDataReader dr = cmd.ExecuteReader();
    if (dr.HasRows)
    {
        rptbio.DataSource = dr;
        rptbio.DataBind();
        dr.Read();
        int a = dr.GetInt32(1);
        dr.Dispose();
        var cmd1 = new SqlCommand("link", conn) { CommandType = CommandType.StoredProcedure };
        cmd1.Parameters.Add(new SqlParameter("@id", SqlDbType.Int)).Value = a;
        SqlDataReader dr1 = cmd1.ExecuteReader();
        rptlink.DataSource = dr1;
        rptlink.DataBind();
        dr1.Dispose();
        conn.Close();
    }
}



我使用dr.read(),但仍然出现此错误!

System.InvalidOperationException:在没有数据的情况下无效的读取尝试

我的sp工作正常,我进行了测试,然后我在网上搜索了!找不到任何内容!



I use dr.read() but I still get this error!

System.InvalidOperationException: Invalid attempt to read when no data is present

my sp work fine i test and i search the web! nothing found!

推荐答案

此代码不显示任何内容.

This code makes no snese.

rptbio.DataSource = dr;
rptbio.DataBind();
dr.Read();
int a = dr.GetInt32(1);
dr.Dispose();



前两行将所有数据读入控件.下一行尝试读取数据,但是所有这些都被读取了,这就是DataBind所做的.



The first two lines read ALL the data in to the control. The next line tries to read data, but it''s ALL alrady been read, that''s what DataBind does.


这篇关于sql datareader问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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