/// <summary> /// 执行SQL 语句,并返回新增记录时的自增ID列表 /// <code> 批量 增/删/改 时用 </code> /// </summary> /// <param name="sqlList">SQL 命令</param> /// <param name="transaction">事务</param> public List <int> Execute(List <string> sqlList, IDbTransaction transaction = null) { if (sqlList == null || sqlList.Count == 0) { return(new List <int>()); } IDbConnection conn = null; List <int> identitys = new List <int>(); int pages = sqlList.Count % _executeSize == 0 ? sqlList.Count / _executeSize : (sqlList.Count / _executeSize + 1); for (int i = 1; i <= pages; i++) { var curSqls = sqlList.Skip((i - 1) * _executeSize).Take(_executeSize); string commandText = string.Join(Environment.NewLine, curSqls); IDbCommand cmd = this.CreateCommand(commandText, transaction); IDataReader reader = null; try { reader = this.ExecuteReader(cmd); conn = cmd != null ? cmd.Connection : null; TypeDeserializer <int> deserializer = new TypeDeserializer <int>(reader, null); do { if (reader.Read()) { identitys.Add(deserializer.Deserialize()); } }while (reader.NextResult()); } finally { Dispose(cmd, reader, conn); } } return(identitys); }
/// <summary> /// 执行SQL 语句,并返回单个实体对象 /// </summary> /// <param name="cmd">SQL 命令</param> /// <param name="define">命令定义对象,用于解析实体的外键</param> /// <returns></returns> public T Execute <T>(IDbCommand cmd, CommandDefine define = null) { IDataReader reader = null; T TResult = default(T); IDbConnection conn = null; try { reader = this.ExecuteReader(cmd); conn = cmd != null ? cmd.Connection : null; TypeDeserializer <T> deserializer = new TypeDeserializer <T>(reader, define as CommandDefine_Select); if (reader.Read()) { TResult = deserializer.Deserialize(); } return(TResult); } finally { Dispose(cmd, reader, conn); } }
/// <summary> /// 执行SQL 语句,并返回 <see cref="IEnumerable"/> 对象 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="cmd">SQL 命令</param> /// <param name="define">命令定义对象,用于解析实体的外键</param> /// <returns></returns> public List <T> ExecuteList <T>(IDbCommand cmd, CommandDefine define = null) { IDataReader reader = null; IDbConnection conn = null; List <T> objList = new List <T>(); try { reader = this.ExecuteReader(cmd); conn = cmd != null ? cmd.Connection : null; TypeDeserializer <T> deserializer = new TypeDeserializer <T>(reader, define as CommandDefine_Select); while (reader.Read()) { objList.Add(deserializer.Deserialize()); } //yield return reader.ToEntity<T>(); } finally { Dispose(cmd, reader, conn); } return(objList); }