各位大虾:
    
    老问题啊(http://topic.csdn.net/u/20080821/13/c8dbb002-1fc4-4b32-ad6e-8bedd43a37da.html)
    我用if(sqldatareader.HasRows)判断后,需要提取sqldatareader记录中某个字段的内容,即 使用sqldatareader["字段名"],但是系统提示“在没有任何数据时进行无效的读取尝试”,看来HasRows并没有读取第一条记录,只好还用sqldatareader.read()做判断,而这样的话读取完第一条记录后就跳到第二条记录了,此时用sqldatareader["字段名"]提取内容是第二条的!! 第一条记录还是读不到,怎么办老大们?
    我看到msdn网站上有一条:http://msdn.microsoft.com/zh-cn/vbasic/system.data.sqlclient.sqldatareader.read(VS.80).aspx
备注:SqlDataReader 的默认位置在第一条记录前面。因此,必须调用 Read 来开始访问任何数据。
按照他说的就不会读不到第一条记录啊,怎么回事啊?

解决方案 »

  1.   

    你读的就是第一条,MSDN说第一条前面,你Read一下,那肯定是第一条了
      

  2.   

    你可以在
    while(rd.read())
    {
      //赋值
      break;
    }
      

  3.   

    read后就是第一条记录了。。
    DataReader好比指针。
    指向的是第一条记录的前面。
    格式就像这样:
    指针   第一条记录   第二条记录  等等。
    你READ后直接就在第一条记录了,你取到的就是。
      

  4.   

    那下面这段代码怎么查询不出记录来(当符合条件的记录只有一条时或者多条时查不出第一条记录)?这个问题真恶心代码如下: 
    Default.aspx: <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > 
    <head runat="server"> 
        <title>无标题页 </title> 
    </head> 
    <body> 
        <form id="form1" runat="server"> 
        <div> 
            <table> 
                <tr> 
                    <td style="width: 100px"> 
                    </td> 
                    <td style="width: 100px"> 
                    </td> 
                    <td style="width: 100px"> 
                    </td> 
                </tr> 
                <tr> 
                    <td style="width: 100px"> 
                        <asp:TextBox ID="cph" runat="server"> </asp:TextBox> </td> 
                    <td style="width: 100px"> 
                        <asp:Button ID="cx" runat="server" OnClick="cx_Click" Text="查询" /> </td> 
                    <td style="width: 100px"> 
                    </td> 
                </tr> 
                <tr> 
                    <td colspan="3"> 
                        <asp:GridView ID="GridView1" runat="server" > 
                        </asp:GridView> 
                    </td> 
                </tr> 
            </table> 
        
        </div> 
        </form> 
    </body> 
    </html> Default.aspx.cs代码如下: using System.Data; 
    using System.Configuration; 
    using System.Web; 
    using System.Web.Security; 
    using System.Web.UI; 
    using System.Web.UI.WebControls; 
    using System.Web.UI.WebControls.WebParts; 
    using System.Web.UI.HtmlControls; 
    using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page 

        protected void Page_Load(object sender, EventArgs e) 
        {     } 
        protected void cx_Click(object sender, EventArgs e) 
        { 
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString); 
            con.Open(); 
            //string cmdtext = "select * from view1 where car_hphm='"+cph+"'"; 
            string ph = cph.Text.ToString(); 
            string cmdtext = "select * from car_memer where car_hphm='"+ph+"'"; 
            //string cmdtext = "select * from car_memer"; 
            SqlCommand cmd = new SqlCommand(cmdtext,con); 
            SqlDataReader dr = cmd.ExecuteReader(); 
            if (dr.Read()) 
            { 
                Response.Write("查询到数据"); 
                this.GridView1.DataSource = dr; 
                this.GridView1.DataBind(); 
                dr.Close(); 
                con.Close(); 
            } 
            else 
            { 
                Response.Write("没有查询到数据"); 
            } 
        } 
    }
      

  5.   

    这样也不行啊,结果集里有两条数据,被read()一读,只显示一条
      

  6.   

    我找到问题出在什么地方了,执行dr.read()后的确是第一条记录的内容,此时用dr["字段明"].tostring()可以读出来,但是绑定数据源后this.GridView1.DataSource = dr;  this.GridView1.DataBind(); 
    GridView1就不显示第一条记录的内容了,有好的解决办法吗?让GridView1显示出第一条记录的内容
      

  7.   

    引用
    if (dr.Read()) 
            { 
                Response.Write("查询到数据"); 
                this.GridView1.DataSource = dr; 
                this.GridView1.DataBind(); 
                dr.Close();    --出错位置:数据还未完全绑定,就已经关了连接
                con.Close(); 
            } 
            else 
            { 
                Response.Write("没有查询到数据"); 
            } 
    晕倒,你用SqlDataReader读取出来的数据,怎么能直接绑定到控件呢?
    SqlDataReader,在读出出来后,要保持与数据库连接的你刚绑定上去,就关闭了联接,而这时候数据还没有完全展示现来,当然会出错啦你如果是这样展示的话,那么建议你
    一、读取到DataSet中,然后再绑定到控件上。
    二、如果读取非要用SqlDataReader,那么你就把这个记录集一条一条加到实现了IList接口的集合中,再绑定数据
      

  8.   

    怎么不能这样,我看的书里面就是这样写的啊,关闭连接后,gridview照样显示数据,只不过显示不了第一条,是因为第一条被read()了以后才绑定到gridview的
      

  9.   

    data reader是在线连接的,关闭之后就不能了吧,用data set 一定可以的,我也不是十分明白
      

  10.   

    if(dr.HasRows)
    连接的数据访问不是说不可以关,而是说使用的时候要一个打开的连接。
    它的同步Execute系列方法是读完后才返回的,所以当然可以关闭了,close是去填充那些output参数,返回值之类的。
    你如果使用BeginExecute系列的就不一样了。