views:

60

answers:

2
       System.Data.SqlClient.SqlConnection foo = new System.Data.SqlClient.SqlConnection();
       foo.ConnectionString = "Data Source=myServer;Integrated Security=SSPI;"; // exception thrown on this line

If I declare a new SqlConnection object in my .net code and then set the ConnectionString field to a syntactically invalid connection string, an exception is thrown.

I have checked several times that if the connection string is semantically incorrect (eg. invalid server name, wrong password), no exception is thrown.

What logic is the SqlConnection object using to validate the connection string at this point? I've tried to no avail to answer this question using RedGate Reflector.

A: 

Connection strings should be a straightforward list of name=value pairs, separated by semicolons (;). What exception are you getting?

If you're using Reflector to try to work out how your string gets parsed, you might find it easier to look at System.Data.Common.DbConnectionStringBuilder. SQL Server's own SqlConnectionStringBuilder inherits from this class.

Tim Robinson
+2  A: 

The validation logic is in the internal SqlConnectionString class's single-string constructor, which it hits via a rather circuitous route.

SqlConnection.ConnectionString's set accessor calls:

SqlConnection.ConnectionString_Set(string);              // Which calls ->
DbConnectionFactory.GetConnectionPoolGroup(string, ...); // Which calls ->
SqlConnectionFactory.CreateConnectionOptions(...);       // Which calls ->
SqlConnectionString(string) // Constructor - take a look at this for details

(Notice that the final call is not to the single-string constructor for SqlConnection - it's constructing an instance of a different class, SqlConnectionString.)

Jeff Sternal
The constructor that takes a string for an argument is just a single line that says `this.ConnectionString = connectionString`. That clashes with your answer.
Rice Flour Cookies
@Rising Star - you're looking at the wrong class. I'm talking about the constructor for `SqlConnectionString`, **not** `SqlConnection`. I've updated my answer to emphasize that.
Jeff Sternal