/// <summary> /// 执行指定的查询语句 /// </summary> /// <param name="sql">参数占位需要自己处理</param> /// <param name="paras">参数</param> /// <returns>若无数据,或参数异常则返回一个元素个数为零的链表</returns> public static List <T> Query <T>(this Table tableObj, String sql, DbParameter[] paras = null, DbConnection connection = null) where T : class { List <T> results = new List <T>(); if (String.IsNullOrEmpty(sql)) { return(results); } Boolean requiredDispose = connection == null; try { connection = connection ?? tableObj.DbProvider.DbConnection(); DbCommand command = tableObj.DbProvider.DbCommand(); ExceutePrepare(connection, command, sql, paras); if (connection.State != ConnectionState.Open) { connection.Open(); } DbDataReader dataReader = command.ExecuteReader(); command.Parameters.Clear(); results = DataExtractor <T> .ToList(dataReader); } finally { if (requiredDispose) { connection.Dispose(); } } return(results); }
public List <T> ToList() { List <T> result = new List <T>(); DbDataReader reader = null; try { reader = this.GetReader(); result = DataExtractor <T> .ToList(reader); } finally { reader?.Close(); this.Close(); } return(result); }