private CPQuery GetUpdateQueryCommand() { IEntityProxy proxy = this as IEntityProxy; if (proxy == null) { throw new InvalidOperationException("请在调用BeginEdit()的返回值对象上调用Update方法。"); } // 获取数据实体对象的主键值,如果数据实体没有指定主键,将会抛出一个异常 Tuple <string, object> rowKey = proxy.GetRowKey(); CPQuery update = GetUpdateQuery(rowKey); if (update == null) { return(null); } CPQuery query = update + " WHERE " + rowKey.Item1 + " = " + new QueryParameter(rowKey.Item2); proxy.ClearChangeFlags(); // 清除修改标记,防止多次调用 return(query); }
/// <summary> /// 执行INSERT操作,并返回新的自增列ID /// </summary> /// <param name="entity"></param> /// <returns></returns> public static int InsertReturnNewId(this Entity entity) { if (entity == null) { throw new ArgumentNullException("entity"); } IEntityProxy proxy = entity as IEntityProxy; if (proxy == null) { throw new InvalidOperationException("当前方法只能在实体代理对象上调用。"); } CPQuery query = entity.GetInsertQuery(); // 在 insert 语句后面加上一个调用。 query = query + "; select SCOPE_IDENTITY();"; proxy.ClearChangeFlags(); // 清除修改标记,防止多次调用 // 执行INSERT操作,并查询最新的ID return(query.ExecuteScalar <int>()); }
/// <summary> /// 根据已修改的实体属性,生成INSERT语句,并执行数据库插入操作, /// 注意:此方法只能在实体的代理对象上调用。 /// </summary> /// <returns>数据库操作过程中影响的行数</returns> public int Insert() { IEntityProxy proxy = this as IEntityProxy; if (proxy == null) { throw new InvalidOperationException("请在调用BeginEdit()的返回值对象上调用Insert方法。"); } CPQuery insert = GetInsertQuery(); if (insert == null) { return(-1); } proxy.ClearChangeFlags(); // 清除修改标记,防止多次调用 return(insert.ExecuteNonQuery()); }
private CPQuery GetInsertQueryCommand() { IEntityProxy proxy = this as IEntityProxy; if (proxy == null) { throw new InvalidOperationException("请在调用BeginEdit()的返回值对象上调用Insert方法。"); } CPQuery insert = GetInsertQuery(); if (insert == null) { return(null); } proxy.ClearChangeFlags(); // 清除修改标记,防止多次调用 return(insert); }
/// <summary> /// 根据已修改的实体属性,生成DELETE查询条件,并执行数据库插入操作, /// 注意:此方法只能在实体的代理对象上调用。 /// </summary> /// <returns>数据库操作过程中影响的行数</returns> public int Delete() { IEntityProxy proxy = this as IEntityProxy; if (proxy == null) { throw new InvalidOperationException("请在调用BeginEdit()的返回值对象上调用Delete方法。"); } CPQuery where = GetWhereQuery(); if (where == null) { return(-1); // 不允许没有WHERE条件的删除,如果确实需要,请手工写SQL } CPQuery query = this.DbContext.CreateCPQuery() + "DELETE FROM " + GetTableName() + where; proxy.ClearChangeFlags(); // 清除修改标记,防止多次调用 return(query.ExecuteNonQuery()); }
private static CPQuery GetInsertReturnNewIdQuery(Entity entity) { if (entity == null) { throw new ArgumentNullException("entity"); } IEntityProxy proxy = entity as IEntityProxy; if (proxy == null) { throw new InvalidOperationException("当前方法只能在实体代理对象上调用。"); } CPQuery query = entity.GetInsertQuery(); // 在 insert 语句后面加上一个调用。 query = query + "; select SCOPE_IDENTITY();"; proxy.ClearChangeFlags(); // 清除修改标记,防止多次调用 return(query); }