一、绑定泛型List<>
我之前接触过的一般都是绑定数据集,这种做法用泛型绑定,非常的方便。
JobService jobBll = new JobService(); List<T_Job> jobList = jobBll.QueryAll(); selJob.DataSource = jobList; selJob.DataTextField = "JobName"; selJob.DataValueField = "JobID"; selJob.DataBind(); selJob.Items.Insert(0, new ListItem("请选择", "0"));
其中JobService是BLL层的一个类,主要是对job的操作
public class JobService : PagerHelper { IJobProvider dal = new JobProvider(); public JobService() { } #region 自定义 /// <summary> /// 查询 /// </summary> /// <returns></returns> public List<T_Job> QueryAll() { return this.GetEntitys<T_Job>("select * from T_Job order by sortIndex", null).ToList(); } #endregion /// <summary> /// 单条插入数据 /// </summary> /// <param name="entity">实体</param> /// <returns></returns> public int Insert(T_Job entity) { if (dal.Insert(entity) > 0) { return 1; } return -1; } /// <summary> /// 批量插入数据 /// </summary> /// <param name="entities">实体</param> /// <returns></returns> public int Insert(IList<T_Job> entities) { if (dal.Insert(entities) > 0) { return 1; } return -1; } /// <summary> /// 单条删除数据 /// </summary> /// <param name="entity"></param> /// <returns></returns> public int Delete(T_Job entity) { return dal.Delete(entity); } /// <summary> /// 批量删除数据 /// </summary> /// <param name="entities"></param> /// <returns></returns> public int Delete(IList<T_Job> entities) { return dal.Delete(entities); } /// <summary> /// 单条更新数据(自动插入数据到任务备注记录表) /// </summary> /// <param name="entity">更改实体</param> /// <returns></returns> public int Update(T_Job entity) { return dal.Update(entity); } /// <summary> /// 批量更新数据(自动插入数据到任务备注记录表) /// </summary> /// <param name="entities">更改实体</param> /// <returns></returns> public int Update(IList<T_Job> entities) { return dal.Update(entities); } /// <summary> /// 传人DateSet获取实体 /// </summary> /// <param name="ds"></param> /// <returns></returns> public IList<T_Job> GetRowEntity(DataSet ds) { return dal.GetRowEntity(ds); } /// <summary> /// 根据id获取结果 /// </summary> /// <param name="id">查询id</param> /// <returns></returns> public T_Job selToID<T>(T JobID) { return dal.selToID(JobID); } }
其中jobBll.QueryAll()作用是查询所有的job并转化为List,注意selJob.Items.Insert(0, new ListItem("请选择", "0"));是一开始是默认为请选择这个