永康企业网站建设公司,能打开所有网址的浏览器,做app和做网站区别,做设计在哪个网站找图片大全一、问题
上文中提到#xff0c;动软提供了数据库的基本操作功能#xff0c;但是往往需要添加新的功能来解决实际问题#xff0c;比如GetModel#xff0c;通过id去查对象#xff1a; 这个功能就需要进行改进#xff1a;往往程序中获取的是实体的其他属性#xff0c;比如…一、问题
上文中提到动软提供了数据库的基本操作功能但是往往需要添加新的功能来解决实际问题比如GetModel通过id去查对象 这个功能就需要进行改进往往程序中获取的是实体的其他属性比如用户登录的时提供账号名和密码需要根据账户名唯一去获取数据库中的实体信息而不是 id 。
二、解决
通过id查对象和通过其他属性查对象应该是非常类似的可以 “照葫芦画瓢 ”
1、先在BLL中添加函数这里利用的是函数重载参数类型不同 对比上下两个函数参数不同而已。
2、在DAL中先找到GetModel函数 很显然拷贝后简单修改一下就可以使用利用账号名查找 的GetModel函数了
3、添加代码 public Maticsoft.Model.user GetModel(string username){StringBuilder strSqlnew StringBuilder();strSql.Append(select id,userID,password,userName from user );strSql.Append( where userIDusername);MySqlParameter[] parameters {new MySqlParameter(userName, MySqlDbType.VarChar)};parameters[0].Value username;Maticsoft.Model.user modelnew Maticsoft.Model.user();DataSet dsDbHelperMySQL.Query(strSql.ToString(),parameters);if(ds.Tables[0].Rows.Count0){return DataRowToModel(ds.Tables[0].Rows[0]);}else{return null;}}
DAL里两个函数的关系 由此对动软生成的功能进行了扩展。比如登录时候的验证代码 [HttpPost]public ActionResult Login(string msg){JObject jobject JObject.Parse(msg);string username (string)jobject[username];string password (string)jobject[password];Maticsoft.BLL.user bll new Maticsoft.BLL.user();string pwd1 UserCookie.Encrypt(password);Maticsoft.Model.user mod bll.GetModel(username);if (mod null){return Content(error);}if (password ! null){string pwd UserCookie.Encrypt(password);if (pwd mod.password){UserCookie.WriteCookie(mod);return Content(OK);}elsereturn Content(error);}return Content(error);}
其中 Maticsoft.Model.user mod bll.GetModel(username);
则是利用提交的用户名唯一去获取数据库中对应记录的信息。拓展功能
以上有对密码加密、写入Cookie等功能后文再介绍。