从数据库读出数据然后以表格形式显示

来源:互联网 发布:淘宝历史成交价格 编辑:程序博客网 时间:2024/05/29 08:42

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace demo3
{
    public partial class SelectAll : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //1连接字符串
            string constr = "data source=.;initial catalog=School;User ID=sa;Password=admin";
            //2创建连接对象
            using (SqlConnection con = new SqlConnection(constr))
            {
                //con.ConnectionString = constr;
                //打开连接


                //拼接sql语句
                string sql = "select * from Student";
                //3创建命令对象
                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    //cmd.Connection = con;
                    //cmd.CommandText = sql;
                    con.Open();//
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    //cmd.ExecuteReader(),调用ExecuteReader()这个方法,就执行了select * from Student。
                    {
                         if(reader.HasRows)//HasRows属性,可以判断这次查询有没有结果集返回。有为true,否则为false
                        {
                            while (reader.Read())
                            {
                                TableRow trRow = new TableRow();
                                //获取第一条记录的第一列中的值。
                                //reader.GetValue(0).ToString();
                                TableCell tcCell = new TableCell();
                                tcCell.BorderStyle = BorderStyle.Solid;
                                tcCell.BorderWidth = 1;
                                tcCell.Text = reader.GetValue(0).ToString();
                                trRow.Cells.Add(tcCell);

                                //获取第一条记录的第2列中的值。
                                tcCell = new TableCell();
                                tcCell.BorderStyle = BorderStyle.Solid;
                                tcCell.BorderWidth = 1;
                                tcCell.Text = reader.GetValue(1).ToString();
                                trRow.Cells.Add(tcCell);
                                //获取第一条记录的第3列中的值。
                                tcCell = new TableCell();
                                tcCell.BorderStyle = BorderStyle.Solid;
                                tcCell.BorderWidth = 1;
                                tcCell.Text = reader.GetValue(2).ToString();
                                trRow.Cells.Add(tcCell);
                                //获取第一条记录的第4列中的值。
                                tcCell = new TableCell();
                                tcCell.BorderStyle = BorderStyle.Solid;
                                tcCell.BorderWidth = 1;
                                tcCell.Text = reader.GetValue(3).ToString();
                                trRow.Cells.Add(tcCell);
                                //获取第一条记录的第5列中的值。
                                tcCell = new TableCell();
                                tcCell.BorderStyle = BorderStyle.Solid;
                                tcCell.BorderWidth = 1;
                                tcCell.Text = reader.GetValue(4).ToString();
                                trRow.Cells.Add(tcCell);
                                //获取第一条记录的第6列中的值。
                                tcCell = new TableCell();
                                tcCell.BorderStyle = BorderStyle.Solid;
                                tcCell.BorderWidth = 1;
                                tcCell.Text = reader.GetValue(5).ToString();
                                trRow.Cells.Add(tcCell);
                                Table1.Rows.Add(trRow);
                           
                            }
                          
                        }
                       
                        // int t = cmd.ExecuteNonQuery();//执行insert,delete ,update正合适
                        //它的返回值就是执行命令后影响数据表中记录的条数。(正整数0---n)
                        //执行了 非insert,delete、update,返回值为-1

                        //this.Response.Write("查询了" + t + "行。");
                    }
                }
            }
        }
    }
}

原创粉丝点击