Where's the Extra Connection String Coming From?

Published Wed, Jun 14 2006 1:33 | William

One of the issues I came across when writing my new book was with ConnectionStrings. Examine the following configuration section:

<connectionStrings>
       <add name="AdventureWorksString"
         providerName="System.Data.SqlClient"
         connectionString="Data Source=localhost;Initial Catalog=db; Integrated Security=true"/>    
  </connectionStrings>

Now, if you run this code snippet, how many connection strings would you expect to apperar:

public static void GetAllConnectionString()
        {
            ConnectionStringSettingsCollection MySettings
                = ConfigurationManager.ConnectionStrings;
            Debug.Assert(MySettings != null);//Should fail if no values are present
            if (MySettings != null)
            {
                StringBuilder sb = new StringBuilder();
                foreach (ConnectionStringSettings individualSettings in MySettings)
                {
                    sb.Append("Full Connection String: " + individualSettings.ConnectionString + "\r\n");
                    sb.Append("Provider Name         : " + individualSettings.ProviderName + "\r\n");
                    sb.Append("Section Name          : " + individualSettings.Name + "\r\n");

                }
                Console.WriteLine(sb.ToString());
            }
        }

If you're like me, you'd expect to see one value.  After all, you added on value to the <connectionstrings> section.  However in most cases, you'll see TWO connection string.  This can be a big issue if you're referencing your connectionstring setting by index instead of name.  the connectionstring you're looking for would be in index 1 instead of 0.  So what gives?  Well, it's pretty simple.  Remember, that you're app.config or web.config file isn't the highest one on the food chain.  Machine.config is.  And in most cases, you'll have a connection string that references the security database that exists by default to handle the Memership API functionality. So in machine.config, you'll probably find..

 <connectionStrings>
    <add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
  </connectionStrings>

So how do you get around this?  First, you can just reference your connection string by name.  That way the index issue isn't an issue at all. The other choice is to use the </clear> element at the beginning of the section:

  <connectionStrings>
    <clear/>
 <--Add your connection strings here-->
  </connectionStrings>

This isn't a really big find, but it's one of those things that didn't behave like I thought it would at first, so i needed to find out why.  Anyway, that's the answer.

Search

This Blog

Tags

Community

Archives

News

My other sites

Cool Stuff

Book Stuff

Security

ORM

Data Access

Funny Stuff

Compact Framework Stuff

Web Casts

My KnowledgeBase Articles

My MVP Profile

Design Patterns

Performance

Debugging

Remoting

My Fellow Authors

My Books

LINQ

Misc

Speech

Syndication

Email Notifications