/// <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); }
/// <summary> /// 执行 SQL 语句,并返回多个实体集合 /// </summary> /// <param name="cmd">SQL 命令</param> /// <param name="defines">命令定义对象,用于解析实体的外键</param> public Tuple <List <T1>, List <T2>, List <T3>, List <T4>, List <T5>, List <T6>, List <T7> > ExecuteMultiple <T1, T2, T3, T4, T5, T6, T7>(IDbCommand cmd, CommandDefine[] defines = null) { IDataReader reader = null; IDbConnection conn = null; List <T1> q1 = null; List <T2> q2 = null; List <T3> q3 = null; List <T4> q4 = null; List <T5> q5 = null; List <T6> q6 = null; List <T7> q7 = null; TypeDeserializer <T1> deserializer1 = null; TypeDeserializer <T2> deserializer2 = null; TypeDeserializer <T3> deserializer3 = null; TypeDeserializer <T4> deserializer4 = null; TypeDeserializer <T5> deserializer5 = null; TypeDeserializer <T6> deserializer6 = null; TypeDeserializer <T7> deserializer7 = null; try { int i = 0; reader = this.ExecuteReader(cmd); conn = cmd != null ? cmd.Connection : null; do { i += 1; while (reader.Read()) { switch (i) { #region 元组赋值 case 1: if (deserializer1 == null) { deserializer1 = new TypeDeserializer <T1>(reader, defines[i - 1] as CommandDefine_Select); } T1 TValue1 = deserializer1.Deserialize(); if (q1 == null) { q1 = new List <T1>(); } q1.Add(TValue1); break; case 2: if (deserializer2 == null) { deserializer2 = new TypeDeserializer <T2>(reader, defines[i - 1] as CommandDefine_Select); } T2 TValue2 = deserializer2.Deserialize(); if (q2 == null) { q2 = new List <T2>(); } q2.Add(TValue2); break; case 3: if (deserializer3 == null) { deserializer3 = new TypeDeserializer <T3>(reader, defines[i - 1] as CommandDefine_Select); } T3 TValue3 = deserializer3.Deserialize(); if (q3 == null) { q3 = new List <T3>(); } q3.Add(TValue3); break; case 4: if (deserializer4 == null) { deserializer4 = new TypeDeserializer <T4>(reader, defines[i - 1] as CommandDefine_Select); } T4 TValue4 = deserializer4.Deserialize(); if (q4 == null) { q4 = new List <T4>(); } q4.Add(TValue4); break; case 5: if (deserializer5 == null) { deserializer5 = new TypeDeserializer <T5>(reader, defines[i - 1] as CommandDefine_Select); } T5 TValue5 = deserializer5.Deserialize(); if (q5 == null) { q5 = new List <T5>(); } q5.Add(TValue5); break; case 6: if (deserializer6 == null) { deserializer6 = new TypeDeserializer <T6>(reader, defines[i - 1] as CommandDefine_Select); } T6 TValue6 = deserializer6.Deserialize(); if (q6 == null) { q6 = new List <T6>(); } q6.Add(TValue6); break; case 7: if (deserializer7 == null) { deserializer7 = new TypeDeserializer <T7>(reader, defines[i - 1] as CommandDefine_Select); } T7 TValue7 = deserializer7.Deserialize(); if (q7 == null) { q7 = new List <T7>(); } q7.Add(TValue7); break; #endregion } } }while (reader.NextResult()); } finally { Dispose(cmd, reader, conn); } return(new Tuple <List <T1>, List <T2>, List <T3>, List <T4>, List <T5>, List <T6>, List <T7> >(q1, q2, q3, q4, q5, q6, q7)); }