Reading keys from sqlServerCatalogNameOverwrites in app.config

Posts   
 
    
JMitchell avatar
JMitchell
User
Posts: 128
Joined: 01-Sep-2006
# Posted on: 25-Jan-2007 11:32:51   

I wish to be able to:

  • switch between databases at compile time without regenerating the code and
  • have the program detect whether it is using the live or development database.

I've done the firs of the above using:

<configSections>
<section name="sqlServerCatalogNameOverwrites" type="System.Configuration.NameValueSectionHandler" />
</configSections>

and

<sqlServerCatalogNameOverwrites>
<add key="NorthwindTest1" value="Northwind1" />
<add key="NorthwindTest2" value="Northwind2" />
</sqlServerCatalogNameOverwrites>

from the help file.

How can I read in and evaluate the keys in sqlServerCatalogNameOverwrites? Everything I have found through searches is restricted to the contents of appSettings

JMitchell avatar
JMitchell
User
Posts: 128
Joined: 01-Sep-2006
# Posted on: 25-Jan-2007 12:20:30   

I sorted it...

Dim devServer As Boolean
Try
    Dim config As System.Collections.Specialized.NameValueCollection = CType(System.Configuration.ConfigurationSettings.GetConfig("sqlServerCatalogNameOverwrites"), System.Collections.Specialized.NameValueCollection)
    Dim x As String = config("NorthwindTest1")
    If x = "Northwind1" Then
devServer = False
    Else
devServer = True
    End If
Catch ex As Exception
    devServer = True
End Try

This assumes that your code is generated for NorthwindTest1 and you can either remove the sqlServerCatalogNameOverwrites section altogether or comment out the key and it should detect it.

There's probably a much simpler way of detecting the catalog name at run time, if you know it, please provide it below as my way is bound to have several holes in it!