23 giugno 2006

ASP.NET 2.0 : Recuperare un connectionString dal Web.Config

La seguente function permette di recuperare (se esiste, altrimenti si ha stringa vuota) una connectionString presente nel web.config:

Private Function GetConnectionString(ByVal ConnectionStringName As String) As String
  Dim cnString As String = ""
  If Not WebConfigurationManager.ConnectionStrings(ConnectionStringName) Is Nothing Then
    cnString = WebConfigurationManager.ConnectionStrings(ConnectionStringName).ConnectionString
  End If
  Return cnString
End Function

La sezione del web.config interessata è la seguente:

  .
  .
  .
<connectionStrings>
  <add name="ConnectionString1" connectionString="............" providerName="System.Data.SqlClient" />
  <add name="ConnectionString2" connectionString="........."
providerName="System.Data.SqlClient" />
  <add name="ConnectionString3" connectionString=".........."
providerName="System.Data.SqlClient" />
</connectionStrings>
  .
  .
  .

Nessun commento: