/// <summary> /// 得到一个对象实体 /// </summary> public Model.oauth_app GetModel(string api_path) { StringBuilder strSql = new StringBuilder(); StringBuilder str1 = new StringBuilder(); Model.oauth_app model = new Model.oauth_app(); //利用反射获得属性的所有公共属性 PropertyInfo[] pros = model.GetType().GetProperties(); foreach (PropertyInfo p in pros) { str1.Append(p.Name + ",");//拼接字段 } strSql.Append("select top 1 " + str1.ToString().Trim(',')); strSql.Append(" from " + databaseprefix + "oauth_app"); strSql.Append(" where api_path=@api_path"); SqlParameter[] parameters = { new SqlParameter("@api_path", SqlDbType.NVarChar, 100) }; parameters[0].Value = api_path; DataTable dt = DbHelperSQL.Query(strSql.ToString(), parameters).Tables[0]; if (dt.Rows.Count > 0) { return(DataRowToModel(dt.Rows[0])); } else { return(null); } }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(Model.oauth_app model) { StringBuilder strSql = new StringBuilder(); StringBuilder str1 = new StringBuilder(); //利用反射获得属性的所有公共属性 PropertyInfo[] pros = model.GetType().GetProperties(); List <SqlParameter> paras = new List <SqlParameter>(); strSql.Append("update " + databaseprefix + "oauth_app set "); foreach (PropertyInfo pi in pros) { //如果不是主键则追加sql字符串 if (!pi.Name.Equals("id")) { //判断属性值是否为空 if (pi.GetValue(model, null) != null) { str1.Append(pi.Name + "=@" + pi.Name + ","); //声明参数 paras.Add(new SqlParameter("@" + pi.Name, pi.GetValue(model, null))); //对参数赋值 } } } strSql.Append(str1.ToString().Trim(',')); strSql.Append(" where id=@id "); paras.Add(new SqlParameter("@id", model.id)); return(DbHelperSQL.ExecuteSql(strSql.ToString(), paras.ToArray()) > 0); }
private bool DoEdit(int _id) { bool result = false; BLL.oauth_app bll = new BLL.oauth_app(); Model.oauth_app model = bll.GetModel(_id); model.title = txtTitle.Text.Trim(); if (cbIsLock.Checked == true) { model.is_lock = 0; } else { model.is_lock = 1; } model.sort_id = Utils.StrToInt(txtSortId.Text.Trim(), 99); model.api_path = txtApiPath.Text.Trim(); model.img_url = txtImgUrl.Text.Trim(); model.remark = txtRemark.Text; if (bll.Update(model)) { AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "修改OAuth信息:" + model.title); //记录日志 result = true; } return(result); }
private bool DoAdd() { bool result = false; Model.oauth_app model = new Model.oauth_app(); BLL.oauth_app bll = new BLL.oauth_app(); model.title = txtTitle.Text.Trim(); if (cbIsLock.Checked == true) { model.is_lock = 0; } else { model.is_lock = 1; } model.sort_id = ConvertTool.ToInt(txtSortId.Text.Trim(), 99); model.api_path = txtApiPath.Text.Trim(); model.img_url = txtImgUrl.Text.Trim(); model.remark = txtRemark.Text; if (bll.Add(model) > 0) { AddAdminLog(DTEnums.ActionEnum.Add.ToString(), "添加OAuth信息:" + model.title); //记录日志 result = true; } return(result); }
private void ShowInfo(int _id) { BLL.oauth_app bll = new BLL.oauth_app(); Model.oauth_app model = bll.GetModel(_id); txtTitle.Text = model.title; if (model.is_lock == 0) { cbIsLock.Checked = true; } else { cbIsLock.Checked = false; } txtSortId.Text = model.sort_id.ToString(); txtApiPath.Text = model.api_path; txtImgUrl.Text = model.img_url; txtRemark.Text = model.remark; }
/// <summary> /// 将对象转换实体 /// </summary> public Model.oauth_app DataRowToModel(DataRow row) { Model.oauth_app model = new Model.oauth_app(); if (row != null) { //利用反射获得属性的所有公共属性 Type modelType = model.GetType(); for (int i = 0; i < row.Table.Columns.Count; i++) { //查找实体是否存在列表相同的公共属性 PropertyInfo proInfo = modelType.GetProperty(row.Table.Columns[i].ColumnName); if (proInfo != null && row[i] != DBNull.Value) { proInfo.SetValue(model, row[i], null);//用索引值设置属性值 } } } return(model); }
/// <summary> /// 增加一条数据 /// </summary> public int Add(Model.oauth_app model) { StringBuilder strSql = new StringBuilder(); StringBuilder str1 = new StringBuilder(); //数据字段 StringBuilder str2 = new StringBuilder(); //数据参数 //利用反射获得属性的所有公共属性 PropertyInfo[] pros = model.GetType().GetProperties(); List <SqlParameter> paras = new List <SqlParameter>(); strSql.Append("insert into " + databaseprefix + "oauth_app("); foreach (PropertyInfo pi in pros) { //如果不是主键则追加sql字符串 if (!pi.Name.Equals("id")) { //判断属性值是否为空 if (pi.GetValue(model, null) != null) { str1.Append(pi.Name + ","); //拼接字段 str2.Append("@" + pi.Name + ","); //声明参数 paras.Add(new SqlParameter("@" + pi.Name, pi.GetValue(model, null))); //对参数赋值 } } } strSql.Append(str1.ToString().Trim(',')); strSql.Append(") values ("); strSql.Append(str2.ToString().Trim(',')); strSql.Append(") "); strSql.Append(";select @@IDENTITY;"); object obj = DbHelperSQL.GetSingle(strSql.ToString(), paras.ToArray()); if (obj == null) { return(0); } else { return(Convert.ToInt32(obj)); } }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(Model.oauth_app model) { return(dal.Update(model)); }
/// <summary> /// 增加一条数据 /// </summary> public int Add(Model.oauth_app model) { return(dal.Add(model)); }