本帖最后由 songwenqi 于 2014-11-27 09:13 编辑
【分布式系统框架教程】-查询单个表数据
导读部分
教程部分
单个表的查询很简单,因为只涉及到一个表,直接查询语句即可,废话不多说,以下是UserDAL层根据userId查询UserInfo对象的代码。
首先配置数据库连接字符串 [C#] 纯文本查看 复制代码 namespace DistributedDAL
{
/// <summary>
/// 所有表的连接字符串都写在这里,方法统一修改
/// </summary>
public static class DbConfig
{
/// <summary>
/// User表的连接字符串
/// </summary>
public static readonly string UserConnection ="UserConnection";
}
} Model层代码 [C#] 纯文本查看 复制代码 namespace DistributedModel.User
{
/// <summary>
/// 用户基类
/// </summary>
public class UserInfo
{
/// <summary>
/// 用户ID
/// </summary>
public int ID { get; set; }
/// <summary>
/// 用户姓名
/// </summary>
public string Name { get; set; }
/// <summary>
/// 用户性别
/// </summary>
public int Sex { get; set; }
/// <summary>
/// 获取用户性别汉字
/// </summary>
/// <returns>汉字男或者女</returns>
public string GetSex()
{
return Gender.GetItem(Sex).Name;
}
/// <summary>
/// 用户手机号
/// </summary>
public string Phone { get; set; }
/// <summary>
/// 所在省
/// </summary>
public int ProvinceId { get; set; }
/// <summary>
/// 获取省汉字
/// </summary>
/// <returns></returns>
public string GetProvince()
{
return Area.GetProvince(ProvinceId).Name;
}
/// <summary>
/// 所在市
/// </summary>
public int CityId { get; set; }
/// <summary>
/// 获取城市汉字
/// </summary>
/// <returns></returns>
public string GetCity()
{
return Area.GetCity(CityId).Name;
}
/// <summary>
/// 12星座ID
/// </summary>
public int ConstellationId { get; set; }
/// <summary>
/// 获取星座汉字
/// </summary>
/// <returns></returns>
public string GetConstellation()
{
return Constellation.GetItem(ConstellationId).Name;
}
/// <summary>
/// 学历ID
/// </summary>
public int DegreeId { get; set; }
/// <summary>
/// 获取学历汉字
/// </summary>
/// <returns></returns>
public string GetDegree()
{
return Degree.GetItem(DegreeId).Name;
}
/// <summary>
/// 民族ID
/// </summary>
public int NationId { get; set; }
/// <summary>
/// 获取民族汉字
/// </summary>
/// <returns></returns>
public string GetNation()
{
return Nation.GetItem(NationId).Name;
}
/// <summary>
/// 生肖ID
/// </summary>
public int ZodiacId { get; set; }
/// <summary>
/// 获取生肖汉字
/// </summary>
/// <returns></returns>
public string GetZodiac()
{
return Zodiac.GetItem(ZodiacId).Name;
}
/// <summary>
/// 添加日期
/// </summary>
public DateTime AddTime { get; set; }
}
}
Dal层代码:其中BaseDAL<UserInfo>是一个基类,里面封装了对数据库操作的各种方法,我们通过代码可以看到UserDAL重写了基类的FillModelFromReader方法以实现自身的数据提取组织对象的处理。 [C#] 纯文本查看 复制代码 namespace DistributedDAL.User
{
public class UserDAL:BaseDAL<UserInfo>
{
/// <summary>
/// 数据库连接字符串名称
/// </summary>
protected override string ConnName
{
get { return DbConfig.UserConnection; }
}
/// <summary>
/// 组织对象
/// </summary>
/// <param name="reader">Reader对象</param>
/// <param name="fields">字段集合</param>
/// <returns></returns>
protected override UserInfo FillModelFromReader(DbDataReader reader, params string[] fields)
{
return ModelFromReader(reader, fields);
}
/// <summary>
/// 组织对象
/// </summary>
/// <param name="reader">Reader对象</param>
/// <param name="fields">字段集合</param>
/// <returns></returns>
protected UserInfo ModelFromReader(DbDataReader reader, params string[] fields)
{
var info = new UserInfo();
if (UtilDAL.HasFields("ID", fields)) info.ID = (int)reader["ID"];
if (UtilDAL.HasFields("ZodiacId", fields)) info.ZodiacId = (int)reader["ZodiacId"];
if (UtilDAL.HasFields("Name", fields)) info.Name = reader["Name"].ToString();
if (UtilDAL.HasFields("Sex", fields)) info.Sex = (int)reader["Sex"];
if (UtilDAL.HasFields("Phone", fields)) info.Phone = reader["Phone"].ToString();
if (UtilDAL.HasFields("ProvinceId", fields)) info.ProvinceId = (int)reader["ProvinceId"];
if (UtilDAL.HasFields("CityId", fields)) info.CityId = (int)reader["CityId"];
if (UtilDAL.HasFields("ConstellationId", fields)) info.ConstellationId = (int)reader["ConstellationId"];
if (UtilDAL.HasFields("DegreeId", fields)) info.DegreeId = (int)reader["DegreeId"];
if (UtilDAL.HasFields("NationId", fields)) info.NationId = (int)reader["NationId"];
if (UtilDAL.HasFields("AddTime", fields)) info.AddTime = (DateTime)reader["AddTime"];
return info;
}
/// <summary>
/// 查找
/// </summary>
/// <param name="userId">ID</param>
/// <returns></returns>
public UserInfo FindById(int userId)
{
var sql = "select * from [User] where ID=@ID";
return FindOne(sql, UtilDAL.CreateParameter("@ID", userId));
}
}
}
Bll层代码 [C#] 纯文本查看 复制代码 namespace DistributedBLL.User
{
public class UserBLL
{
UserDAL dal=new UserDAL();
/// <summary>
/// 获取一个对象根据userId
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
public UserInfo FindById(int userId)
{
return dal.FindById(userId);
}
}
}
前端代码 首先我们需要在web.config文件里配置一下UserInfo的数据库连接字符串
[C#] 纯文本查看 复制代码 <connectionStrings>
<!--User表的连接字符串-->
<add name="UserConnection" connectionString="server=192.168.18.100;uid=sa;password=1234;database=testDB;" providerName="System.Data.SqlClient"/>
</connectionStrings>
然后前段我们就可以调用这个方法实现单表查询
[C#] 纯文本查看 复制代码 UserInfo user = userbll.FindById(userId);
效果如下
|