public static bool DeleteByOnly <T>(object only) { var type = typeof(T); var st = type.GetTblStruct(); string sql = "delete from " + st.TblName; var key = Convert.ToString(only).SqlClear(); sql += " where " + st.OnlyPrimary + "=" + (st.TypeOnlyPrimary ? key : ("'" + key + "'")); var isSuccess = DbHelperAccess.ExecuteCommand(sql) > 0; return(isSuccess); }
public static bool UpdateByOnly <T>(T t) { var type = typeof(T); var st = type.GetTblStruct(); var p = type.GetProperty(st.ClassOnlyPrimary); var obj = p.GetValue(t, null); var key = Convert.ToString(obj).SqlClear(); string sql = GetUpdateString(t, st.OnlyPrimary); sql += " where " + st.OnlyPrimary + "=" + (st.TypeOnlyPrimary ? key : ("'" + key + "'")); var isSuccess = DbHelperAccess.ExecuteCommand(sql) > 0; return(isSuccess); }
/// <summary> /// 主键是自增,则返回主键ID /// 不是,则返回影响行数 /// 默认一般是>0就是成功 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="t"></param> /// <returns></returns> public static int Add <T>(T t) { lock (_sycn) { var type = typeof(T); var st = type.GetTblStruct(); string insertCol = string.Empty; string values = string.Empty; foreach (var item in st.ListCol) { if (st.Identity) //自增的主键,主键就不赋值 { if (item.PrimaryKey) { continue; } } if (item.NotAdd) { continue; } insertCol += item.ColName + ","; var p = type.GetProperty(item.ClassColName); var obj = p.GetValue(t, null); if (obj == null) { values += "NULL,"; } else { values += "'" + Convert.ToString(obj).SqlClear() + "',"; } } if (insertCol.Length > 0) { insertCol = insertCol.Remove(insertCol.Length - 1); } if (values.Length > 0) { values = values.Remove(values.Length - 1); } string sql = "insert into " + st.TblName + "(" + insertCol + ") values(" + values + ");"; if (st.Identity) { var count2 = DbHelperAccess.ExecuteCommand(sql); if (count2 > 0) { sql = "select @@identity;"; object obj = DbHelperAccess.GetScalar(sql); if (obj == null) { return(0); } var id = Convert.ToInt32(obj); var p = type.GetProperty(st.ClassPrimaryKey); p.SetValue(t, id, null); return(id); } return(0); } var count = DbHelperAccess.ExecuteCommand(sql); return(count); } }