/// <summary> /// 执行查询 /// </summary> public static DbDataReader ExecuteReader(DbCommand command, CommandBehavior behavior = CommandBehavior.Default, bool?needSaveLog = null) { try { DateTime dateTime = DateTime.Now; try { DbConnectionHelper.OpenConnection(command.Connection); return(command.ExecuteReader(behavior)); } finally { if (needSaveLog.HasValue && needSaveLog.Value || !needSaveLog.HasValue && AppRun.Debugging) { Task.Run(() => EventLog.Save(String.Format("{0} consume time {1} ms", PackCommandInfo(command), DateTime.Now.Subtract(dateTime).TotalMilliseconds))); } } } catch (Exception ex) { if (needSaveLog.HasValue && needSaveLog.Value || !needSaveLog.HasValue && AppRun.Debugging) { Task.Run(() => EventLog.Save(PackCommandInfo(command), ex)); } throw; } }
/// <summary> /// 执行查询,并返回查询所返回的结果集中第一行的第一列 /// </summary> /// <returns>返回值</returns> public static object ExecuteScalar(DbCommand command, bool?needSaveLog = null) { try { DateTime dateTime = DateTime.Now; try { DbConnectionHelper.OpenConnection(command.Connection); object result = command.ExecuteScalar(); return(result == null || DBNull.Value.Equals(result) ? null : result); } finally { if (needSaveLog.HasValue && needSaveLog.Value || !needSaveLog.HasValue && AppRun.Debugging) { Task.Run(() => EventLog.Save(String.Format("{0} consume time {1} ms", PackCommandInfo(command), DateTime.Now.Subtract(dateTime).TotalMilliseconds))); } } } catch (Exception ex) { if (needSaveLog.HasValue && needSaveLog.Value || !needSaveLog.HasValue && AppRun.Debugging) { Task.Run(() => EventLog.Save(PackCommandInfo(command), ex)); } throw; } }
/// <summary> /// 填充 DataSet /// </summary> /// <param name="command">DbCommand</param> /// <param name="needSaveLog">是否记录日志</param> public static DataSet FillDataSet(DbCommand command, bool?needSaveLog = null) { #if PgSQL using (DbDataAdapter adapter = new NpgsqlDataAdapter()) #endif #if MsSQL using (DbDataAdapter adapter = new SqlDataAdapter()) #endif #if MySQL using (DbDataAdapter adapter = new MySqlDataAdapter()) #endif #if ORA using (DbDataAdapter adapter = new OracleDataAdapter()) #endif { adapter.SelectCommand = command; DateTime dateTime = DateTime.Now; try { try { DbConnectionHelper.OpenConnection(command.Connection); DataSet result = new DataSet(); adapter.Fill(result); return(result); } finally { if (needSaveLog.HasValue && needSaveLog.Value || !needSaveLog.HasValue && AppRun.Debugging) { Task.Run(() => EventLog.Save(String.Format("{0} consume time {1} ms", PackCommandInfo(command), DateTime.Now.Subtract(dateTime).TotalMilliseconds))); } } } catch (Exception ex) { if (needSaveLog.HasValue && needSaveLog.Value || !needSaveLog.HasValue && AppRun.Debugging) { Task.Run(() => EventLog.Save(PackCommandInfo(command), ex)); } throw; } } }