ConfigurationManager.ConnectionStrings Proprietà

Definizione

Ottiene i dati di ConnectionStringsSection per la configurazione predefinita dell'applicazione corrente.Gets the ConnectionStringsSection data for the current application's default configuration.

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

Valore della proprietà

Oggetto ConnectionStringSettingsCollection che contiene il contenuto dell'oggetto ConnectionStringsSection per la configurazione predefinita dell'applicazione corrente.A ConnectionStringSettingsCollection object that contains the contents of the ConnectionStringsSection object for the current application's default configuration.

Eccezioni

Non è stato possibile recuperare un oggetto ConnectionStringSettingsCollection.Could not retrieve a ConnectionStringSettingsCollection object.

Esempio

Nell'esempio seguente viene illustrato come utilizzare una stringa di connessione per leggere i dati da un database.The following example shows how to use a connection string to read data from a database.

using System;  
using System.Configuration;  
using System.Data.SqlClient;  
  
namespace ConsoleApplication1  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            ReadProducts();  
        }  
  
        static void ReadProducts()  
        {  
            var connectionString = ConfigurationManager.ConnectionStrings["WingtipToys"].ConnectionString;  
            string queryString = "SELECT Id, ProductName FROM dbo.Products;";  
            using (var connection = new SqlConnection(connectionString))  
            {  
                var command = new SqlCommand(queryString, connection);  
                connection.Open();  
                using (var reader = command.ExecuteReader())  
                {  
                    while (reader.Read())  
                    {  
                        Console.WriteLine(String.Format("{0}, {1}", reader[0], reader[1]));  
                    }  
                }  
            }  
        }  
    }  
}  
Imports System.Configuration  
Imports System.Data.SqlClient  
Module Module1  
  
    Sub Main()  
        ReadProducts()  
    End Sub  
  
    Sub ReadProducts()  
        Dim connectionString = ConfigurationManager.ConnectionStrings("WingtipToys").ConnectionString  
        Dim queryString = "SELECT Id, ProductName FROM dbo.Products;"  
        Using connection As New SqlConnection(connectionString)  
            Dim command = New SqlCommand(queryString, connection)  
            connection.Open()  
  
            Using reader As SqlDataReader = command.ExecuteReader()  
                While reader.Read()  
                    Console.WriteLine(String.Format("{0}, {1}", reader(0), reader(1)))  
                End While  
            End Using  
        End Using  
    End Sub  
  
End Module  

Nell'esempio precedente si presuppone che il progetto disponga di un file app. config, come illustrato di seguito.The previous example assumes your project has an App.config as shown below.

<?xml version="1.0" encoding="utf-8" ?>  
<configuration>  
    <startup>   
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />  
    </startup>  
    <connectionStrings>  
      <add name="WingtipToys" connectionString="Data Source=(LocalDB)\v11.0;Initial Catalog=WingtipToys;Integrated Security=True;Pooling=False" />  
    </connectionStrings>  
</configuration>  

Commenti

Un oggetto ConnectionStringsSection contiene il contenuto della sezione connectionStrings del file di configurazione.A ConnectionStringsSection object contains the contents of the configuration file's connectionStrings section.

Si applica a

Vedi anche