/// <summary> /// 更新一条数据 /// </summary> public bool Update(Model.user_code model) { StringBuilder strSql = new StringBuilder(); StringBuilder str1 = new StringBuilder(); //利用反射获得属性的所有公共属性 PropertyInfo[] pros = model.GetType().GetProperties(); List <MySqlParameter> paras = new List <MySqlParameter>(); strSql.Append("update " + databaseprefix + "user_code 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 MySqlParameter("@" + pi.Name, pi.GetValue(model, null))); //对参数赋值 } } } strSql.Append(str1.ToString().Trim(',')); strSql.Append(" where id=@id "); paras.Add(new MySqlParameter("@id", model.id)); return(DbHelperMySql.ExecuteSql(strSql.ToString(), paras.ToArray()) > 0); }
/// <summary> /// 根据用户名得到一个对象实体 /// </summary> public Model.user_code GetModel(string user_name, string code_type, string datepart) { StringBuilder strSql = new StringBuilder(); StringBuilder str1 = new StringBuilder(); Model.user_code model = new Model.user_code(); //利用反射获得属性的所有公共属性 PropertyInfo[] pros = model.GetType().GetProperties(); foreach (PropertyInfo p in pros) { str1.Append(p.Name + ",");//拼接字段 } strSql.Append("select top 1 " + str1.ToString().Trim(',') + " from " + databaseprefix + "user_code"); strSql.Append(" where status=0 and datediff(" + datepart + ",eff_time,getdate())<=0 and user_name=@user_name and type=@type"); MySqlParameter[] parameters = { new MySqlParameter("@user_name", MySqlDbType.VarChar, 100), new MySqlParameter("@type", MySqlDbType.VarChar, 20) }; parameters[0].Value = user_name; parameters[1].Value = code_type; DataSet ds = DbHelperMySql.Query(strSql.ToString(), parameters); if (ds.Tables[0].Rows.Count > 0) { return(DataRowToModel(ds.Tables[0].Rows[0])); } else { return(null); } }
/// <summary> /// 得到一个对象实体 /// </summary> public Model.user_code GetModel(int id) { StringBuilder strSql = new StringBuilder(); StringBuilder str1 = new StringBuilder(); Model.user_code model = new Model.user_code(); //利用反射获得属性的所有公共属性 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 + "user_code"); strSql.Append(" where id=@id"); MySqlParameter[] parameters = { new MySqlParameter("@id", MySqlDbType.Int32, 4) }; parameters[0].Value = id; DataTable dt = DbHelperMySql.Query(strSql.ToString(), parameters).Tables[0]; if (dt.Rows.Count > 0) { return(DataRowToModel(dt.Rows[0])); } else { return(null); } }
/// <summary> /// 根据生成码得到一个对象实体 /// </summary> public Model.user_code GetModel(string str_code) { StringBuilder strSql = new StringBuilder(); StringBuilder str1 = new StringBuilder(); Model.user_code model = new Model.user_code(); //利用反射获得属性的所有公共属性 PropertyInfo[] pros = model.GetType().GetProperties(); foreach (PropertyInfo p in pros) { str1.Append(p.Name + ",");//拼接字段 } strSql.Append("select top 1 " + str1.ToString().Trim(',') + " from " + databaseprefix + "user_code"); strSql.Append(" where status=0 and datediff(d,eff_time,getdate())<=0 and str_code=@str_code"); SqlParameter[] parameters = { new SqlParameter("@str_code", SqlDbType.NVarChar, 255) }; parameters[0].Value = str_code; DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters); if (ds.Tables[0].Rows.Count > 0) { return(DataRowToModel(ds.Tables[0].Rows[0])); } else { return(null); } }
/// <summary> /// 将对象转换实体 /// </summary> public Model.user_code DataRowToModel(DataRow row) { Model.user_code model = new Model.user_code(); 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.user_code model) { StringBuilder strSql = new StringBuilder(); StringBuilder str1 = new StringBuilder(); //数据字段 StringBuilder str2 = new StringBuilder(); //数据参数 //利用反射获得属性的所有公共属性 PropertyInfo[] pros = model.GetType().GetProperties(); List <MySqlParameter> paras = new List <MySqlParameter>(); strSql.Append("insert into " + databaseprefix + "user_code("); 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 MySqlParameter("@" + 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 = DbHelperMySql.GetSingle(strSql.ToString(), paras.ToArray()); if (obj == null) { return(0); } else { return(Convert.ToInt32(obj)); } }