Esempio n. 1
0
    /// <summary>
    /// Gets the ConnectionManager object for the 
    /// specified database.
    /// </summary>
    /// <param name="database">
    /// The database name or connection string.
    /// </param>
    /// <param name="isDatabaseName">
    /// True to indicate that the connection string
    /// should be retrieved from the config file. If
    /// False, the database parameter is directly 
    /// used as a connection string.
    /// </param>
    /// <param name="label">Label for this connection.</param>
    /// <returns>ConnectionManager object for the name.</returns>
    public static ConnectionManager GetManager(string database, bool isDatabaseName, string label)
    {
      if (isDatabaseName)
      {
        var connection = ConfigurationManager.ConnectionStrings[database];
        if (connection == null)
          throw new ConfigurationErrorsException(String.Format(Resources.DatabaseNameNotFound, database));

        var conn = ConfigurationManager.ConnectionStrings[database].ConnectionString;
        if (string.IsNullOrEmpty(conn))
          throw new ConfigurationErrorsException(String.Format(Resources.DatabaseNameNotFound, database));
        database = conn;
      }

      lock (_lock)
      {
        var ctxName = GetContextName(database, label);
        ConnectionManager mgr = null;
        if (ApplicationContext.LocalContext.Contains(ctxName))
        {
          mgr = (ConnectionManager)(ApplicationContext.LocalContext[ctxName]);

        }
        else
        {
          mgr = new ConnectionManager(database, label);
          ApplicationContext.LocalContext[ctxName] = mgr;
        }
        mgr.AddRef();
        return mgr;
      }
    }