/// <summary> /// 生成command对象 /// </summary> /// <param name="batch"></param> /// <returns></returns> internal protected DbCommand BuildDbCommand(SqlBatch batch) { var sql = new StringBuilder(); var paramters = CreateParamterCollection(); EntityExplain entityExplain = null; //统一生成sql之后统一去执行 foreach (var item in batch) { if (item.ItemType == ItemCommandType.Text) { var sqlStat = (string)item.Command; if (item.Parameters == null || item.Parameters.Length == 0) { sql.Append(sqlStat).AppendLine(); } else { var paras = paramters.Fill(item.Parameters); sql.AppendFormat(sqlStat, paras).AppendLine(); } continue; } // 实体转换成sql var entity = item.Command; var entyExpl = entity as IEntityExplain; if (entyExpl == null) { if (entityExplain == null) { entityExplain = new EntityExplain(entity); } else { entityExplain.Switch(entity); } } bool hasIncrement; var innerSql = EntityToSql.GenerateInsertOrUpdate(entyExpl ?? entityExplain, paramters, false, out hasIncrement); if (string.IsNullOrEmpty(innerSql)) { continue; } sql.AppendLine(innerSql); } // 没有sql语句,则直接退出。 if (sql.Length == 0) { return(null); } // return(BuildDbCommand(CommandType.Text, sql.ToString(), paramters)); }
/// <summary> /// 执行sql语句,返回DataSet。 /// </summary> /// <param name="batch">sql批次</param> /// <param name="transaction">事务处理</param> /// <returns>受影响的行数</returns> /// <remarks><paramref name="batch"/> 中包含增量标识的实体对象,不会返回自增序列的值。</remarks> public DataSet ExecuteDataSet(SqlBatch batch, DbTransaction transaction) { var command = BuildDbCommand(batch); return(ExecuteDataSet(command, transaction)); }
/// <summary> /// 执行sql语句,返回DataSet。 /// </summary> /// <param name="batch">sql批次</param> /// <returns>受影响的行数</returns> /// <remarks><paramref name="batch"/> 中包含增量标识的实体对象,不会返回自增序列的值。</remarks> public DataSet ExecuteDataSet(SqlBatch batch) { var command = BuildDbCommand(batch); return(ExecuteDataSet(command)); }
/// <summary> /// 执行sql语句,不返回任何结果集。 /// </summary> /// <param name="batch">sql批次</param> /// <param name="transaction">事务处理</param> /// <returns>受影响的行数</returns> /// <remarks><paramref name="batch"/> 中包含增量标识的实体对象,不会返回自增序列的值。</remarks> public int ExecuteNonQuery(SqlBatch batch, DbTransaction transaction) { var command = BuildDbCommand(batch); return(ExecuteNonQuery(command, transaction)); }
/// <summary> /// 执行sql语句,不返回任何结果集。 /// </summary> /// <param name="batch">sql批次</param> /// <returns>受影响的行数</returns> /// <remarks><paramref name="batch"/> 中包含增量标识的实体对象,不会返回自增序列的值。</remarks> public int ExecuteNonQuery(SqlBatch batch) { var command = BuildDbCommand(batch); return(ExecuteNonQuery(command)); }
/// <summary> /// 执行sql语句,返回IDataReader。 /// </summary> /// <param name="batch">sql批次</param> /// <param name="transaction">事务处理</param> /// <returns>受影响的行数</returns> /// <remarks><paramref name="batch"/> 中包含增量标识的实体对象,不会返回自增序列的值。</remarks> public IDataReader ExecuteReader(SqlBatch batch, DbTransaction transaction) { var command = BuildDbCommand(batch); return(ExecuteReader(command, transaction)); }
/// <summary> /// 执行sql语句,返回IDataReader。 /// </summary> /// <param name="batch">sql批次</param> /// <returns>受影响的行数</returns> /// <remarks><paramref name="batch"/> 中包含增量标识的实体对象,不会返回自增序列的值。</remarks> public IDataReader ExecuteReader(SqlBatch batch) { var command = BuildDbCommand(batch); return(ExecuteReader(command)); }