WebConfigurationManager.ConnectionStrings Propiedad

Definición

Obtiene las cadenas de conexión del sitio web.

public:
 static property System::Configuration::ConnectionStringSettingsCollection ^ ConnectionStrings { System::Configuration::ConnectionStringSettingsCollection ^ get(); };
public static System.Configuration.ConnectionStringSettingsCollection ConnectionStrings { get; }
static member ConnectionStrings : System.Configuration.ConnectionStringSettingsCollection
Public Shared ReadOnly Property ConnectionStrings As ConnectionStringSettingsCollection

Valor de propiedad

Objeto ConnectionStringSettingsCollection que contiene los elementos del objeto ConnectionStringsSection correspondientes a la configuración predeterminada de la aplicación Web actual.

Excepciones

No se pudo recuperar un objeto ConnectionStringSettingsCollection válido.

Ejemplos

En el ejemplo siguiente se muestra cómo usar la propiedad para obtener acceso a la ConnectionStrings información de configuración y enumerar los resultados. Para acceder a una cadena de conexión específica, use el devuelto ConnectionStringSettingsCollection con el nombre de la cadena de conexión deseada como indizador.


// Show the use of the ConnectionString property
// to get the connection strings.
static void GetConnectionStrings()
{

    // Get the connectionStrings key,value pairs collection.
    ConnectionStringSettingsCollection connectionStrings =
        WebConfigurationManager.ConnectionStrings
        as ConnectionStringSettingsCollection;

    // Get the collection enumerator.
    IEnumerator connectionStringsEnum =
        connectionStrings.GetEnumerator();

    // Loop through the collection and 
    // display the connectionStrings key, value pairs.
    int i = 0;
    Console.WriteLine("[Display connectionStrings]");
    while (connectionStringsEnum.MoveNext())
    {
        string name = connectionStrings[i].Name;
        Console.WriteLine("Name: {0} Value: {1}",
        name, connectionStrings[name]);
        i += 1;
    }

    Console.WriteLine();
}
' Show the use of ConnectionStrings property
' to get the connection strings. 
Shared Sub GetConnectionStrings()
   
   ' Get the connectionStrings key,value pairs collection.
     Dim connectionStrings As ConnectionStringSettingsCollection = _
     WebConfigurationManager.ConnectionStrings
   
   ' Get the collection enumerator.
     Dim connectionStringsEnum As IEnumerator = _
     connectionStrings.GetEnumerator()
   
   ' Loop through the collection and 
   ' display the connectionStrings key, value pairs.
   Dim i As Integer = 0
   Console.WriteLine("[Display connectionStrings]")
   While connectionStringsEnum.MoveNext()
      Dim name As String = connectionStrings(i).Name
         Console.WriteLine("Name: {0} Value: {1}", _
         name, connectionStrings(name))
      i += 1
   End While
   
   Console.WriteLine()
End Sub

Se aplica a

Consulte también