/// <summary> /// Method for invoking a specified Database service object. Reads service settings /// from the ConnectionSettings.config file. /// </summary> /// <example> /// <code> /// Database dbSvc = DatabaseFactory.CreateDatabase("SQL_Customers"); /// </code> /// </example> /// <param name="config">IConfiguration supplies connection string information to database factory</param> /// <param name="name">configuration key for database service</param> /// <returns>Database</returns> /// <exception cref="System.Configuration.ConfigurationException"> /// <para>- or -</para> /// <para>An error exists in the configuration.</para> /// <para>- or -</para> /// <para>An error occured while reading the configuration.</para> /// </exception> /// <exception cref="System.Reflection.TargetInvocationException"> /// <para>The constructor being called throws an exception.</para> /// </exception> public static IDBConnection CreateDatabase(string name) { try { name = name.ToLowerInvariant(); var mappedName = GetConnectionName(name); var connectionString = _config.GetConnectionString(mappedName); if (string.IsNullOrEmpty(connectionString)) { if (mappedName == name) { throw new ConfigurationErrorsException($"could not locate connectionString: {name}"); } throw new ConfigurationErrorsException($"could not locate connectionString: {name} mapped to {mappedName}"); } var connection = new SqlDBConnection(connectionString) { DBCode = mappedName }; return(connection); } catch (ConfigurationErrorsException configurationException) { if (Logger.HandleException(LoggingBoundaries.DataLayer, configurationException)) { throw; } throw; } }
internal DBCommand(SqlDBConnection dbconn, string commandText, CommandType commandType) { Initialize(); _dbconn = dbconn; _procname = commandText; _commandType = commandType; _params = new Dictionary <string, DbParameter>(); }
private void executeProcReader(Action <IDataReader> predicate, SqlDBConnection dbConn, bool sequential) { using (var connection = new SqlConnection(dbConn.ConnectionString)) { //create a command and prepare it for execution using (var command = new SqlCommand { CommandTimeout = _dbconn.CommandTimeout, Connection = connection }) { _dbconn.LastSql = string.Empty; try { using (Logger.CreateTrace(LoggingBoundaries.Database, "executeProcReader", _schema, _procname)) { //pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache) SqlParameter[] commandParameters = dbConn.GetParameters(_schema, _procname); /* foreach (var def in _defaults) * { * if (!_params.ContainsKey(def.Key)) * AddInParameter(def.Key, def.Value); * }*/ //assign the provided values to these parameters based on parameter order _dbconn.LastSql = dbConn.PrepareCommand(command, CommandType.StoredProcedure, _schema, _procname, commandParameters, _params.Values.ToArray()); Logger.LogTrace(LoggingBoundaries.Database, "Execute Reader Called:\n{0}", _dbconn.LastSql); if (connection.State != ConnectionState.Open) { connection.Open(); } using (SqlDataReader dr = sequential ? command.ExecuteReader(CommandBehavior.SequentialAccess) : command.ExecuteReader()) { predicate(dr); } } } catch (Exception ex) { var ex2 = new SqlDBException(ex, command, _dbconn.LastSql); if (Logger.HandleException(LoggingBoundaries.Database, ex2)) { throw ex2; } } } } }