不使用ORM工具时的取出对象集合
来源:互联网 发布:java开发电脑配置 编辑:程序博客网 时间:2024/06/03 09:19
public static List<Dept> GetDepts(DateTime? limitDate) { var depts = new List<Dept>(); string strSql; DataTable dataTable; if (limitDate == null) { strSql = "SELECT d.DeptId,deptName,COUNT(dt.TeamId) FROM Dept d left join DeptTeam dt on d.DeptId = dt.DeptId where d.endDate is null group by d.DeptId,deptName"; dataTable = DbConnector.GenerateDataTable(strSql); } else { strSql = "SELECT d.DeptId,deptName,COUNT(dt.TeamId) FROM Dept d left join DeptTeam dt on d.DeptId = dt.DeptId where (d.endDate>@limitDate or d.endDate is null) group by d.DeptId,deptName"; dataTable = DbConnector.GenerateDataTable(strSql, new[] { new SqlParameter("@limitDate", limitDate) }); } if(dataTable!=null&&dataTable.Rows.Count>0) { for(int i=0;i<dataTable.Rows.Count;i++) { depts.Add(new Dept(Convert.ToInt32(dataTable.Rows[i][0]),dataTable.Rows[i][1].ToString(),Convert.ToInt32(dataTable.Rows[i][2]))); } } return depts; }