创建实体模型:
namespace Test.Models { public class Student { public string ID { get; set; } public string Name { get; set; } } }
使用:
using System; using System.Data; using System.Collections.Generic; namespace Test { public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { List<Models.Student> studentlist = new List<Models.Student>(); string sql = string.Format("select ID, Name from Tb_Student where Sex = {0}", 1); DataTable dt = new DataTable(); dt = DAL.SQLHelper.ExecuteTable(sql, CommandType.Text); if (dt != null || dt.Rows.Count >0) { for (int i = 0; i < dt.Rows.Count; i++) { Models.Student student = new Models.Student { ID = dt.Rows[i]["ID"].ToString(), Name = dt.Rows[i]["Name"].ToString() }; studentlist.Add(student); } } this.GridView1.DataSource = studentlist; this.DataBind(); } } }
时间: 2024-10-11 05:20:43