一:我的问题:
UI执行插入 和 更新 时 只对EbUser user = new EbUser();的部分字段赋值,而参数是整个user.
对于没有赋值的字段
可能是在日后的用户信息完善中由用户自己维护
也可能是数据库本身设定有默认值 例如dateCreated字段在定义的时候有默认值getdate()
请问对于这个问题,插入时该如何处理?对于没有赋值的字段 以及有默认值的字段该如何处理?
二、解决方案基本情况如下:
1、表结构
CREATE TABLE EbUser(
userId int IDENTITY(1,1) NOT NULL Primary Key, */
userName nvarchar(50) NULL,
nickName nvarchar(50) NULL,
userImg nvarchar(50) NULL,
phone nvarchar(50) NULL,
mobile nvarchar(50) NULL,
IdCard nvarchar(50) NULL,
email varchar(50) NULL,
qq varchar(50) NULL,
city nvarchar(50) NULL,
addr nvarchar(50) NULL,
postCode varchar(50) NULL,
userPwd varchar(50) NULL,
dateCreated date NULL DEFAULT (getdate()) ,
roleId Int null,
levelId Int null,
memo NVarchar(200) null
)
2、业务对象类
public class EbUser { public int userId {get;set;} public string userName {get;set;} public string nickName {get;set;} public string userPwd {get;set;} public string phone {get;set;} public string mobile {get;set;} public string IdCard {get;set;} public string email {get;set;} public string qq {get;set;} public string city {get;set;} public string addr {get;set;} public string postCode {get;set;} public DateTime dateCreated {get;set;} public int roleId {get;set;} public int levelId {get;set;} public string memo {get;set;} }
3、数据访问层Dao
/// <summary> /// 添加dbo.EbUser /// </summary> /// <param name="ebUser">dbo.EbUser</param> /// <returns>最新编号</returns> public static int AddEbUser(EbUser ebUser) { string sql = "INSERT INTO EbUser(userName,nickName,userPwd,phone,mobile,IdCard,email,qq,city,addr,postCode,dateCreated,roleId,levelId,memo)" + " values(@userName,@nickName,@userPwd,@phone,@mobile,@IdCard,@email,@qq,@city,@addr,@postCode,@dateCreated,@roleId,@levelId,@memo);" + " SELECT SCOPE_IDENTITY();"; try { SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@userName",SqlDbType.NVarChar,50), new SqlParameter("@nickName",SqlDbType.NVarChar,50), new SqlParameter("@userPwd",SqlDbType.VarChar,50), new SqlParameter("@phone",SqlDbType.NVarChar,50), new SqlParameter("@mobile",SqlDbType.NVarChar,50), new SqlParameter("@IdCard",SqlDbType.NVarChar,50), new SqlParameter("@email",SqlDbType.VarChar,50), new SqlParameter("@qq",SqlDbType.VarChar,50), new SqlParameter("@city",SqlDbType.NVarChar,50), new SqlParameter("@addr",SqlDbType.NVarChar,50), new SqlParameter("@postCode",SqlDbType.VarChar,50), new SqlParameter("@dateCreated",SqlDbType.DateTime,8), new SqlParameter("@roleId",SqlDbType.Int,4), new SqlParameter("@levelId",SqlDbType.Int,4), new SqlParameter("@memo",SqlDbType.NVarChar,200) }; int i=0; paras[i++].Value = ebUser.userName; paras[i++].Value = ebUser.nickName; paras[i++].Value = ebUser.userPwd; paras[i++].Value = ebUser.phone; paras[i++].Value = ebUser.mobile; paras[i++].Value = ebUser.IdCard; paras[i++].Value = ebUser.email; paras[i++].Value = ebUser.qq; paras[i++].Value = ebUser.city; paras[i++].Value = ebUser.addr; paras[i++].Value = ebUser.postCode; paras[i++].Value = ebUser.dateCreated; paras[i++].Value = ebUser.roleId; paras[i++].Value = ebUser.levelId; paras[i++].Value = ebUser.memo; return Convert.ToInt32(SqlHelper.ExecuteCommand(sql, paras)); } catch (Exception e) { Console.WriteLine(e.Message); throw e; } }
4、业务逻辑层BLL
public static int AddEbUser(EbUser ebUser) { return EbUserService.AddEbUser(ebUser); }
6、UI界面层代码
protected void Regist_Click(object sender, EventArgs e)
{
EbUser user = new EbUser();
user.userName = UserName.Text.ToString().Trim();
user.userPwd = Wangzhl.DecryptString(UserName.Text.ToString().Trim());
user.city = GetClientInfo.GetCityByIp(GetClientInfo.GetIp());
EbUserManager.AddEbUser(user)
}
7、我的问题
UI执行插入时 只对EbUser user = new EbUser();的部分字段赋值,而参数是整个user.
对于没有赋值的字段
可能是在日后的用户信息完善中由用户自己维护
也可能是数据库本身设定有默认值 例如dateCreated字段在定义的时候有默认值getdate()
请问对于这个问题,插入时该如何处理?对于没有赋值的字段 以及有默认值的字段该如何处理?
- 4 回答
- 0 关注
- 501 浏览
添加回答
举报
0/150
提交
取消