在三层架构中,Bll层不允许出现类似DataTable这种在DAL层出现的数据,所以必须在DAL层对DataTable进行转换
例如把数据行打包成User数据实体(每一行即是一个业务实体):
- /// <summary>
- /// 把数据行打包成User数据实体
- /// </summary>
- /// <param name="dataRow">一条记录</param>
- /// <returns>User实体</returns>
- public static User GetUserFromDataRow(DataRow dataRow)
- {
- Models.User user = new Models.User();
- if (dataRow["userID"].ToString() != "")
- {
- user.UserID = int.Parse(dataRow["userID"].ToString());
- }
- user.UserName = dataRow["userName"].ToString();
- user.UserAlias = dataRow["userAlias"].ToString();
- user.UserPassword = dataRow["userPassword"].ToString();
- user.UserEmail = dataRow["userEmail"].ToString();
- if (dataRow["userGroupID"].ToString() != "")
- {
- user.UserGroupID = int.Parse(dataRow["userGroupID"].ToString());
- }
- if (dataRow["userCreateTime"].ToString() != "")
- {
- user.UserCreateTime = DateTime.Parse(dataRow["userCreateTime"].ToString());
- }
- if (dataRow["userScore"].ToString() != "")
- {
- user.UserScore = int.Parse(dataRow["userScore"].ToString());
- }
- if (dataRow["userTopicNum"].ToString() != "")
- {
- user.UserTopicNum = int.Parse(dataRow["userTopicNum"].ToString());
- }
- if (dataRow["userPostNum"].ToString() != "")
- {
- user.UserPostNum = int.Parse(dataRow["userPostNum"].ToString());
- }
- if (dataRow["isDeleted"].ToString() != "")
- {
- if ((dataRow["isDeleted"].ToString() == "1") || (dataRow["isDeleted"].ToString().ToLower() == "true"))
- {
- user.IsDeleted = true;
- }
- else
- {
- user.IsDeleted = false;
- }
- }
- if (dataRow["isForbidden"].ToString() != "")
- {
- if ((dataRow["isForbidden"].ToString() == "1") || (dataRow["isForbidden"].ToString().ToLower() == "true"))
- {
- user.IsForbidden = true;
- }
- else
- {
- user.IsForbidden = false;
- }
- }
- return user;
- }
要获取所有的数据行,可以用泛型集合List
- /// <summary>
- /// 获得所有用户列表
- /// </summary>
- public static List<User> GetUserList()
- {
- List<User> userList = new List<Models.User>();
- StringBuilder strSql = new StringBuilder();
- strSql.Append("SELECT *");
- strSql.Append("FROM [user] ");
- DataSet dataSet = SqlHelper.ExecuteDataset(strSql.ToString());
- if (dataSet.Tables[0].Rows.Count > 0)
- {
- foreach (DataRow row in dataSet.Tables[0].Rows)
- {
- userList.Add(GetUserFromDataRow(row));
- }
- return userList;
- }
- return null;
- }
在BLL层返回该List,访问该List即可
- /// <summary>
- /// 获得所有用户列表
- /// </summary>
- /// <returns></returns>
- public static List<User> GetUserList()
- {
- return UserDal.GetUserList();
- }