|
本帖最后由 songwenqi 于 2014-11-28 09:06 编辑
【分布式系统框架教程】-添加一条数据
导读部分
教程部分
上几篇帖子已经提到过,各种准备工作,首先需要配置数据库连接字符串,这里就不再贴代码了,LoginUserInfo.cs和UserInfo.cs两个model类也不在贴了,可以到查询两个表之间链接表的数据去看一下这两个类,这篇主要讲一下如何添加数据,下面贴出LoginUserDAL.cs和UserDAL.cs的添加方法,我只贴相关代码,具体例子,喜欢的亲们可以去购买我们的分布式框架结合教程,就一目了然了。 [C#] 纯文本查看 复制代码 namespace DistributedDAL.User
{
public class LoginUserDAL : BaseDAL<LoginUserInfo>
{
/// <summary>
/// 数据库连接字符串名称
/// </summary>
protected override string ConnName
{
get { return DbConfig.LoginUserConnection; }
}
/// <summary>
/// 组织对象
/// </summary>
/// <param name="reader">Reader对象</param>
/// <param name="fields">字段集合</param>
/// <returns></returns>
protected override LoginUserInfo FillModelFromReader(DbDataReader reader, params string[] fields)
{
return ModelFromReader(reader, fields);
}
/// <summary>
/// 组织对象
/// </summary>
/// <param name="reader">Reader对象</param>
/// <param name="fields">字段集合</param>
/// <returns></returns>
protected LoginUserInfo ModelFromReader(DbDataReader reader, params string[] fields)
{
var info = new LoginUserInfo();
if (UtilDAL.HasFields("ID", fields)) info.ID = (int)reader["ID"];
if (UtilDAL.HasFields("UserName", fields)) info.UserName = reader["UserName"].ToString();
if (UtilDAL.HasFields("UserPwd", fields)) info.UserPwd = reader["UserPwd"].ToString();
if (UtilDAL.HasFields("UserStatus", fields)) info.UserStatus = (UserStatus)(int)reader["UserStatus"];
if (UtilDAL.HasFields("LoginIp", fields)) info.LoginIp = reader["LoginIp"].ToString();
if (UtilDAL.HasFields("LoginType", fields)) info.LoginType = (LoginType)(int)reader["LoginType"];
return info;
}
/// <summary>
/// 添加
/// </summary>
/// <param name="info">LoginUserInfo对象</param>
/// <returns></returns>
public int AddLoginUserInfo(LoginUserInfo info)
{
var sql = "INSERT INTO [LoginUser]([ID],[UserName],[UserPwd],[UserStatus],[LoginIp],[LoginType])"
+ "VALUES(@Id,@UserName,@UserPwd,@UserStatus,@LoginIp,@LoginType)";
return Execute(sql, UtilDAL.CreateParameter("@Id", info.ID),
UtilDAL.CreateParameter("@UserName", info.UserName),
UtilDAL.CreateParameter("@UserPwd", info.UserPwd),
UtilDAL.CreateParameter("@UserStatus", info.UserStatus),
UtilDAL.CreateParameter("@LoginIp", info.LoginIp),
UtilDAL.CreateParameter("@LoginType", info.LoginType));
}
} UserDAL.cs: [C#] 纯文本查看 复制代码 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="user">UserInfo对象</param>
/// <returns>返回当前插入的对象ID</returns>
public int AddUserInfo(UserInfo user)
{
var sql = "INSERT INTO [User] ([Name] ,[Sex] ,[Phone] ,[ProvinceId] ,[CityId] ,[ConstellationId] ,[DegreeId] ,[NationId] ,[ZodiacId] ) "
+ "VALUES (@Name,@Sex,@Phone,@ProvinceId,@CityId,@ConstellationId,@DegreeId,@NationId,@ZodiacId);SELECT @@IDENTITY as ID";
object ret=GetScalar(sql,UtilDAL.CreateParameter("@Name",user.Name),
UtilDAL.CreateParameter("@Sex",user.Sex),
UtilDAL.CreateParameter("@Phone",user.Phone),
UtilDAL.CreateParameter("@ProvinceId",user.ProvinceId),
UtilDAL.CreateParameter("@CityId",user.CityId),
UtilDAL.CreateParameter("@ConstellationId",user.ConstellationId),
UtilDAL.CreateParameter("@DegreeId",user.DegreeId),
UtilDAL.CreateParameter("@NationId",user.NationId),
UtilDAL.CreateParameter("@ZodiacId",user.ZodiacId));
return Convert.ToInt32(ret);
}
} 以下是bll层代码 [C#] 纯文本查看 复制代码 public class LoginUserBLL
{
LoginUserDAL dal = new LoginUserDAL();
/// <summary>
/// 添加一条记录
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
public int AddLoginUserInfo(LoginUserInfo info)
{
return dal.AddLoginUserInfo(info);
}
}
[C#] 纯文本查看 复制代码 public class UserBLL
{
UserDAL dal=new UserDAL();
/// <summary>
/// 添加一条记录
/// </summary>
/// <param name="user">UserInfo对象</param>
/// <returns>返回当前插入对象ID</returns>
public int AddUserInfo(UserInfo user)
{
return dal.AddUserInfo(user);
}
}
好了,准备工作已经完成,下面是web端的代码了: [C#] 纯文本查看 复制代码 UserBLL userbll=new UserBLL(); LoginUserBLL loginbll=new LoginUserBLL();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindSelect();//这个以后会专门有一篇将如何使用静态集合
}
}
protected void btnOk_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
Add();
}
}
public void Add()
{
if (Request["id"] == null)
{
//从页面输入信息组织UserInfo对象
UserInfo user = GetUser();
//添加成功返回当前对象的唯一标识ID
int userId = userbll.AddUserInfo(user);
if (userId > 0)
{
//从页面输入信息组织LoginUserInfo对象(传递唯一标识ID)
LoginUserInfo loginuser = GetLoginUser(userId);
//添加LoginUserInfo对象
loginbll.AddLoginUserInfo(loginuser);
Response.Redirect("list.aspx");
}
}
}
/// <summary>
/// 获取UserInfo对象 通过页面 此时返回的对象ID未赋值 修改操作时需赋ID值
/// </summary>
/// <returns></returns>
public UserInfo GetUser()
{
string name = txtUserName.Value;
string phone = txtPhone.Value.Trim(); ;
string sex = ddlGender.Value;
string zodiac = ddlZodiac.Value;
string constellation = ddlConstellation.Value;
string degree = ddlDegree.Value;
string nation = ddlNation.Value;
string provice = ddlProv.Value;
string city = ddlCity.Value;
UserInfo user = new UserInfo
{
Name = name,
Phone = phone,
Sex = InputHelper.GetInputInt(sex),
ZodiacId = InputHelper.GetInputInt(zodiac),
ConstellationId = InputHelper.GetInputInt(constellation),
DegreeId = InputHelper.GetInputInt(degree),
NationId = InputHelper.GetInputInt(nation),
ProvinceId = InputHelper.GetInputInt(provice),
CityId = InputHelper.GetInputInt(city)
};
return user;
}
/// <summary>
/// 获取LoginUserInfo对象
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
public LoginUserInfo GetLoginUser(int userId)
{
string loginUser = txtLoginUserName.Value;
string userStatus = ddlUserStatus.Value;
string pwd = txtPwd.Value;
string loginType = ddlLoginType.Value;
string loginIP = Request.UserHostAddress;
LoginUserInfo loginuser = new LoginUserInfo
{
ID = userId,
UserName = loginUser,
UserPwd = pwd,
LoginIp = loginIP,
UserStatus = (UserStatus)int.Parse(userStatus),
LoginType = (LoginType)int.Parse(loginType)
};
return loginuser;
}
效果如下:
|
|