2009-12-07 5 views
2

Quelqu'un pourrait-il fournir un exemple de code, montrant comment intégrer le fichier app.config à une application écrite en VB.NET? Plus précisément, j'ai besoin d'extraire les chaînes de connexion à la base de données à partir du fichier app.config.VB.NET Exemple app.config Accès

Répondre

1

Voir:

Public Shared Function GetConnectionString(ByVal strConnection As String) As String 
    'Declare a string to hold the connection string 
    Dim sReturn As New String("") 
    'Check to see if they provided a connection string name 
    If Not String.IsNullOrEmpty(strConnection) Then 
     'Retrieve the connection string fromt he app.config 
     sReturn = ConfigurationManager. & _ 
     ConnectionStrings(strConnection).ConnectionString 
    Else 
     'Since they didnt provide the name of the connection string 
     'just grab the default on from app.config 
     sReturn = ConfigurationManager. & _ 
     ConnectionStrings("YourConnectionString").ConnectionString 
    End If 
    'Return the connection string to the calling method 
    Return sReturn 
End Function