/// <summary> /// 失败返回null /// </summary> public override T Select <T>(SelectBuilder selectBuilder) { if (selectBuilder == null) { throw new NotImplementedException("缺少有效的" + nameof(SelectBuilder)); } T result = new T(); DbCommand command = Session.CreateCommand(); command.CommandText = selectBuilder.ToQueryString(Session, new T().TableName); selectBuilder.AddParameter(command, Session); WriteQueryLog(command, Session); using (var reader = Session.ExecuteDataReader(command)) { if (reader.Read()) { var fields = selectBuilder.ComponentSelect.GetSelectFields(); if (fields.Count() == 0) { result.Init(reader); } else { result.Init(reader, fields); } return(result); } else { return(null); } } }
/// <summary> /// 未查询到数据时返回 null /// </summary> public override DbDataReader SelectAsDataReader <T>(SelectBuilder selectBuilder) { if (selectBuilder == null) { throw new NotImplementedException("缺少有效的" + nameof(SelectBuilder)); } DbCommand command = Session.CreateCommand(); command.CommandText = selectBuilder.ToQueryString(Session, new T().TableName); selectBuilder.AddParameter(command, Session); WriteQueryLog(command, Session); return(Session.ExecuteReader(command)); }
/// <summary> /// 未查询到数据时返回 null /// </summary> public override List <string> SelectAsStrings <T>(SelectBuilder selectBuilder) { if (selectBuilder == null) { throw new NotImplementedException("缺少有效的" + nameof(SelectBuilder)); } DbCommand command = Session.CreateCommand(); command.CommandText = selectBuilder.ToQueryString(Session, new T().TableName); selectBuilder.AddParameter(command, Session); WriteQueryLog(command, Session); var reader = Session.ExecuteReader(command); List <string> result = new List <string>(); var field = selectBuilder.ComponentSelect.GetSelectFields().First(); while (reader.Read()) { result.Add(reader[field] as string); } return(result); }
/// <summary> /// 未查询到数据时返回 null /// </summary> public override string SelectAsString <T>(SelectBuilder selectBuilder) { if (selectBuilder == null) { throw new NotImplementedException("缺少有效的" + nameof(SelectBuilder)); } DbCommand command = Session.CreateCommand(); command.CommandText = selectBuilder.ToQueryString(Session, new T().TableName); selectBuilder.AddParameter(command, Session); WriteQueryLog(command, Session); var data = Session.ExecuteScalar(command); if (data != null) { return(data.ToString()); } else { return(null); } }