C#中(WEB)查询SQL数据库返回多行,myreadertemp.Read()怎么控制显示在网页上?

解决方案 »

  1.   

     sqlreader? 只向前读的,, 一行一行获取, 写进去, 记得要关闭连接
      

  2.   

    使用SqlDataAdapter.Fill 方法 然后去页面直接绑定。
      

  3.   

    SqlDataAdapter.Fill 到dataset里,然后处理
      

  4.   

    使用循环读出到IList<T>,到页面用gridview绑定
      

  5.   

    如果按一定的规则取数的话,想返回多条数据的方法很多,可以使用SQLDATAREADER一行一行的遍历或者使用LIST<类>来整成一个泛型列表,然后可以在这个集合中可以使用LINQ再查询....
      

  6.   

    C#中(WEB)查询SQL数据库返回多行,myreadertemp.Read()怎么控制显示在网页上?第一种:老的方式,直接循环出

    SqlDataReader dr=myReader;///这里是你的dataReader
    while(dr.Read()){
      Response.Write(dr["字段名称"].ToString());
    }第二种方式:在前台用控件如Repeater,,然后在后台bind
    前台
    <asp:Repeater Runat="server" ID="MyDataRepeater">
    <ItemTemplate>
    <%#Eval("字段名称")%>
    </ItemTemplate>
    </asp:Repeater>
    protected void Page_Load(object sender,EventArgs e){
      if(!IsPostBack){
        this.MyDataRepeater.DataSource=reader;
        this.MyDataRepeater.DataBind();
      }
    }
      

  7.   

    直接fill到dataset中,然后循环
      

  8.   

    我也是用 到dataset的方法的
      

  9.   

    建议不用 sqlreader  对于很多情况 sqlreader 真的很不好用 最关键不好用的就是连接关闭的问题 当然 也可以返回一个LIST<> 差不多
     /// <summary>
        /// 返回集合类型的查询 也可以单个查询
        /// </summary>
        /// <param name="sqlstr">SQL语句</param>
        /// <param name="para">参数</param>
        /// <returns></returns>
        public DataTable ReturnAll(string sqlstr, SqlParameter[] para)
        {    
             DataTable dt = new DataTable();
            using (SqlConnection conn = new SqlConnection(sqlconn))
            {
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = sqlstr;
                comm.Parameters.AddRange(para);
                SqlDataAdapter sa = new SqlDataAdapter();
                sa.SelectCommand=comm;
                try
                {
                    conn.Open();
                    sa.Fill(dt);
                    conn.Close();
                }
                catch (Exception ex)
                {
                    conn.Close();
                    throw (ex);
                }
                finally
                {
                    conn.Close();
                }
                       }        return dt;
        }  public DataTable SelectPicture(int id)
        {
            string str = "select img,width,height from AllPicture where id=@one";
            SqlParameter[] sp = new SqlParameter[] {new SqlParameter("@one",id) };
            DataTable dt =ReturnAll(str,sp);
            return dt;
        }
    Datatable怎么绑定在Gridview上··不用说了吧?