int DeleteModel(IMODEL obj) { int executeNum = 0; if (obj != null) { Type type = obj.GetType(); PropertyInfo[] p = type.GetProperties(); List <KeyBox> kbox = GetKeyList(p, obj); List <DataParameter> listPara = new List <DataParameter>(); string whereStr = ""; try { int js = 0; foreach (KeyBox box in kbox) { whereStr += (js == 0 ? "" : " AND ") + box.Key + "=" + box.Dbpara.ParameterName; listPara.Add(new DataParameter(box.Dbpara.ParameterName, box.Dbpara.Value)); js++; } } catch (Exception exp) { throw exp; } executeNum = ExecuteNonQuery("DELETE FROM " + GetTableName(obj.GetType()) + " WHERE " + whereStr, listPara); } return(executeNum); }
int AddModel(IMODEL obj) { int executeNum = 0; if (obj != null) { List <DataParameter> listPara = new List <DataParameter>(); Type type = obj.GetType(); string info = ""; string infoValue = ""; PropertyInfo[] p = type.GetProperties(); int i = 0; try { foreach (PropertyInfo propInfo in p) { if (propInfo.GetValue(obj, null) != null) { if (i > 0) { info += ",[" + propInfo.Name + "]"; infoValue += ",@" + propInfo.Name + "" + ""; } else { info += "[" + propInfo.Name + "]"; infoValue += "@" + propInfo.Name + "" + ""; } string paravalue = propInfo.GetValue(obj, null) + ""; if (propInfo.PropertyType.FullName.ToLower().IndexOf("datetime") != -1) { paravalue = DateTime.Parse(paravalue).ToString("yyyy-MM-dd"); } listPara.Add(new DataParameter("@" + propInfo.Name, paravalue)); i++; } } } catch (Exception exp) { throw exp; } executeNum = ExecuteNonQuery("INSERT INTO " + GetTableName(obj.GetType()) + " (" + info + ") VALUES(" + infoValue + ")", listPara); } return(executeNum); }
public void CopyMyToModel(IMODEL obj, List <string> notKey) { PropertyInfo[] p = obj.GetType().GetProperties(); foreach (PropertyInfo toinfo in p) { foreach (PropertyInfo pinfo in this.GetType().GetProperties()) { if (toinfo.Name == pinfo.Name && notKey.IndexOf(pinfo.Name) == -1) { if (pinfo.GetValue(this, null) != null) { toinfo.SetValue(obj, pinfo.GetValue(this, null), null); } break; } } } }
/// <summary> /// 将本对象赋值给其他对象 /// </summary> /// <param name="obj"></param> public void CopyMyToModel(IMODEL obj) { List <string> list = new List <string>(); list.Add("ItemNo"); list.Add("KeyID"); list.Add("OrgKey"); list.Add("CreateUserID"); list.Add("CreateTime"); list.Add("UpdateUserID"); list.Add("UpdateTime"); list.Add("DeleteUserID"); list.Add("DeleteTime"); list.Add("ConfirmStatus"); list.Add("Status"); list.Add("CheckedStatusCode"); list.Add("isCheckedUserID"); list.Add("CheckedTime"); list.Add("isUnChecked"); list.Add("UnCheckedUserID"); list.Add("UnCheckedTime"); CopyMyToModel(obj, list); }
int DeleteModel(IMODEL obj) { dbContext.Entry(obj).State = EntityState.Deleted; return(1); }
int UpdateModel(IMODEL obj) { dbContext.Entry(obj).State = EntityState.Modified; return(1); }
int AddModel(IMODEL obj) { obj = (IMODEL)dbContext.Set(obj.GetType()).Add(obj); return(1); }
public string GetObjKey(IMODEL obj) { return(Guid.NewGuid().ToString()); }
int UpdateModel(IMODEL obj) { int executeNum = 0; if (obj != null) { Type type = obj.GetType(); string info = ""; PropertyInfo[] p = type.GetProperties(); List <DataParameter> listPara = new List <DataParameter>(); List <KeyBox> kbox = GetKeyList(p, obj); foreach (KeyBox item in kbox) { if (string.IsNullOrEmpty(item.Dbpara.Value + "")) { throw new Exception("未将主键赋值"); } } int i = 0; try { foreach (PropertyInfo propInfo in p) { //object[] list = propInfo.GetCustomAttributes(true).Where(infos => infos.GetType().Name == "ModelAttribute").ToArray(); List <object> list = new List <object>(); foreach (var item in propInfo.GetCustomAttributes(true)) { if (item.GetType().Name == "ModelAttribute") { list.Add(item); } } if (list.Count != 0) { ModelAttribute matt = list[0] as ModelAttribute; if (propInfo.GetValue(obj, null) != null && !matt.ModelContent.IsKey) { info += (i != 0 ? "," : "") + "[" + propInfo.Name + "]=@" + propInfo.Name; DbParameter par = dbComm.CreateParameter(); par.ParameterName = "@" + propInfo.Name; string paravalue = propInfo.GetValue(obj, null) + ""; if (propInfo.PropertyType.FullName.ToLower().IndexOf("datetime") != -1) { paravalue = DateTime.Parse(paravalue).ToString("yyyy-MM-dd"); } listPara.Add(new DataParameter("@" + propInfo.Name, paravalue)); i++; } } } } catch (Exception exp) { throw exp; } string UpdateStr = "UPDATE " + GetTableName(obj.GetType()) + " SET " + info; if (kbox.Count != 0) { int js = 0; foreach (KeyBox box in kbox) { UpdateStr += (js == 0 ? " WHERE " : " AND ") + box.Key + "=@" + box.Key; listPara.Add(new DataParameter(box.Dbpara.ParameterName, box.Dbpara.Value)); js++; } executeNum = ExecuteNonQuery(UpdateStr, listPara); } } return(executeNum); }