public virtual int Update() { Type type = base.GetType(); FieldItem identityField = FieldItem.GetIdentityField(type); if (identityField == null) { throw new Exception("使用该方法需要配置Identity字段"); } IList <FieldItem> dataObjectFields = FieldItem.GetDataObjectFields(type); SqlCommand cmd = new SqlCommand(); StringBuilder sb = new StringBuilder(); foreach (FieldItem item in dataObjectFields) { if ((item.DataObjectField != null) && !item.DataObjectField.IsIdentity) { if (sb.Length > 0) { sb.Append(","); } sb.AppendFormat("[{0}]=@{0}", item.ColumnName); cmd.Parameters.AddWithValue("@" + item.ColumnName, type.GetProperty(item.ColumnName).GetValue(this, null)); } } string sql = string.Format(@"UPDATE {0} SET {1} WHERE [{2}]=@{2}", FieldItem.GetTableName(type), sb.ToString(), identityField.ColumnName); cmd.Parameters.AddWithValue("@" + identityField.ColumnName, type.GetProperty(identityField.ColumnName).GetValue(this, null)); cmd.CommandText = sql; return(Database.ExecuteNonQuery(cmd)); }
public static int QueryCount(string where) { return(QueryCount(FieldItem.GetTableName(typeof(T)), where)); }
public static int QueryCount() { return(QueryCount(FieldItem.GetTableName(typeof(T)), string.Empty)); }