DataSet,DataTable,List实现将Sql Server表中的数据绑定到GradView中


-------------DataSet,DataTable--------


前台:
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
        <br />
        <asp:Button ID="Button1" runat="server" οnclick="Button1_Click" Text="Button" />
    </div>
    </form>
</body>




后台:
     protected void Button1_Click(object sender, EventArgs e)
        {
            string constr = "data source=.;initial catalog=Test1;User id=sa;password=111111";
            string sql = "select * from MyStudents";
            SqlDataAdapter adpter = new SqlDataAdapter(sql, constr);


            #region    adpter到数据集dataset中
            //DataSet ds = new DataSet();
            //adpter.Fill(ds);
            //GridView1.DataSource = ds;
            //GridView1.DataBind();
            #endregion


            #region   adpter到数据表DataTable中
            DataTable dt = new DataTable();
            adpter.Fill(0, 10, dt);//显示表中的前十行
            GridView1.DataSource = dt;
            GridView1.DataBind();
            #endregion
        }


-------------list-----------
前台:
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
        <br />
        <asp:Button ID="btnListGridView" runat="server" Text="Button" 
            οnclick="btnListGridView_Click" />
    </div>
    </form>
</body>



Student类:
  public class Student
    {
        public int Fid { get; set; }
        public string Fname { get; set; }
        public int Fage { get; set; }
        public bool Fgender { get; set; }
        public int Fmath { get; set; }
        public int Fenglish { get; set; }
        public DateTime Fbirthday { get; set; }
    }


后台:
        protected void btnListGridView_Click(object sender, EventArgs e)
        {
            string connStr = "data source=.;initial catalog=Test1;User id=sa;password=111111";


            using (SqlConnection conn = new SqlConnection(connStr))
            {
                string sqlStr = "select * from MyStudents";
                using (SqlCommand cmd = new SqlCommand(sqlStr, conn))
                {
                    conn.Open();
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        List<Student> list = new List<Student>();

                        while (reader.Read())
                        {

                            // 表中字段: Fid  Fname Fage  Fgender  Fmath  Fenglish  Fbirthday 

                            Student sInfo = new Student();
                            sInfo.Fid = reader.GetInt32(reader.GetOrdinal("FId"));
                            sInfo.Fname = reader.GetString(reader.GetOrdinal("FName"));
                            sInfo.Fage = reader.GetInt32(reader.GetOrdinal("fAge"));
                            sInfo.Fgender = reader.GetBoolean(reader.GetOrdinal("FGender"));
                            sInfo.Fmath = reader.GetInt32(reader.GetOrdinal("FMath"));
                            sInfo.Fenglish = reader.GetInt32(reader.GetOrdinal("FEnglish"));
                            sInfo.Fbirthday = reader.GetDateTime(reader.GetOrdinal("FBirthday"));
                            list.Add(sInfo);
                        }
                    }
                }
                GridView1.DataSource = GetUserInfoAll(connStr, sqlStr);
                GridView1.DataBind();
            }
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值