none
How to read connection strings from app.config RRS feed

  • Question

  • In asp.net, it's easy:

    dim con as new data.sqlclient.sqlconnection(System.configuration.configurationmanager.ConnectionStrings("ConnectionString").tostring)

     

    does the trick

     

    In a client application though, ConfigurationManager is not recognized.  So in the app.config I put:

     

    <connectionStrings>

    <add name="ParkerReportingValidation.MySettings.ReportingConnectionString"

    connectionString="Data Source=SATURN;Initial Catalog=ParkerReporting;User ID=sa;Pooling=False"/>

    </connectionStrings>

     

    and try to read the connection string with

    Dim ConString as string = My.Settings.ReportingConnectionString

     

    but it doesn't work.

     

    I see the error ReportingConnectionString is not a member of ParkerReportingValidation.My.MySettings.  From my reading, it seems I'm not alone.  What's the trick?

    Thursday, August 2, 2007 1:08 AM

Answers

  • I think I'm in love.

     

    The secret was adding a reference to system.configuration.  My final code looks like this, and I know it will work because I do this all the time in asp.net

     

    In the app.config:

    <configuration>

    <connectionStrings>

    <add name="ParkerReporting"

    connectionString="Data Source=SATURN;Initial Catalog=ParkerReporting;User ID=sa;Pooling=False"/>

    </connectionStrings>

     

    In the actual form:

    Dim con As New System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("ParkerReporting").ToString)

     

    In an asp.net application, when the web.config file is created, by default it adds a namespace for system.configuration in the <namespaces> tag of the <pages> tag.  In an application, app.config does not add a <namespaces> tag by default.  Food for thought.

     

    Thursday, August 2, 2007 11:56 AM

All replies

  • Joe,

     

    1. Add a reference to System.Configuration

     

    2. Add the proper SQL connection string to app.config

     

    Code Snippet

    <appSettings>

    <add key="ParkerSQLConnection" value="Data Source=SATURN;Initial Catalog=ParkerReporting;Persist Security Info=True;User ID=sa;Password=**************************************" />

    </appSettings>

     

     

     

    3. Execute the following code:

     

    Code Snippet

    Imports System.Data.SqlClient

    Module Module1

    Sub Main()

    Dim sqlConnection As New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("ParkerSQLConnection"))

    Dim sqlCommand As New SqlCommand("SELECT GETDATE()")

    Try

    With sqlCommand

    .Connection = sqlConnection

    .CommandType = CommandType.Text

    End With

    sqlConnection.Open()

    Console.WriteLine(sqlCommand.ExecuteScalar)

    Catch ex As Exception

    Console.WriteLine(ex.Message)

    Finally

    sqlConnection.Close()

    sqlCommand.Dispose()

    sqlConnection.Dispose()

    End Try

    End Sub

    End Module

     

    Thursday, August 2, 2007 4:41 AM
  • I think I'm in love.

     

    The secret was adding a reference to system.configuration.  My final code looks like this, and I know it will work because I do this all the time in asp.net

     

    In the app.config:

    <configuration>

    <connectionStrings>

    <add name="ParkerReporting"

    connectionString="Data Source=SATURN;Initial Catalog=ParkerReporting;User ID=sa;Pooling=False"/>

    </connectionStrings>

     

    In the actual form:

    Dim con As New System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("ParkerReporting").ToString)

     

    In an asp.net application, when the web.config file is created, by default it adds a namespace for system.configuration in the <namespaces> tag of the <pages> tag.  In an application, app.config does not add a <namespaces> tag by default.  Food for thought.

     

    Thursday, August 2, 2007 11:56 AM