SqlConnectionStringBuilder

Published Mon, Jan 3 2005 19:03 | William

You know, working with connection strings has never been 'hard' but it's always been a pain in the ass- especially when you know all the information but you don't remember the exact order.  This is hardly the most impressive thing I've come across in ADO.NET 2.0 - but I really like it - and if you're using Yukon - there's another cool little feature:


SqlConnectionStringBuilder sb = new SqlConnectionStringBuilder();
sb.DataSource =
"Bill_2k3";
sb.InitialCatalog =
"Northwind";
sb.IntegratedSecurity =
true;
sb.Encrypt =
true;
SqlConnection cn = new SqlConnection(sb.ToString());
cn.Open();
cn.Close();

So as long as you know the server name and DB you want to work with - you can get cranking (assuming you're using Trusted Connection.  But the whole way of dealing with it seems so much more RDBMS.  The whole ConnectionString thing has always seemed so anti-Relational and Non -OOP because it wreaks with MultiValued fieldness.  Sure - managing connectionstrings is not exactly a huge problem threatening to bring the industry to its knees, but this is one of those nice features that makes working in Visual Studio .NET 2005 more pleasant to deal with - and there's tons of them.  It also typifies that even though the ADO.NET 2.0 team is working hard to bring us some hard core stuff- they still pay attention to detail as far as user experience goes.

BTW, this is hardly an exhaustive example - there's a ton more stuff in it like FailOver partner, which I haven't gotten to work yet but feel I'm on the verge of success any minute now.

Comments

# William said on January 3, 2005 7:22 PM:

Hey Bill,

I just got hold of the SQL Server 2005 resource kit and it IS wicked!!

best feature so far, now that i've just installed it, is that it will run on Win XP Pro - yeah baby!

Also not to mention that the server image (win 2003) comes with it pre-installed plus contain a copy of Visual Studio .Net 2005.

If you don't have it already (or if anybody else is looking), you can order it here -> http://msstore.datacom.com.au/sqlbeta

# William said on January 3, 2005 7:22 PM:

Looks useful, I hate working with Connection Strings, they really go against the grain. Nice to see an OO approach to the subject!

# William said on January 3, 2005 9:02 PM:

Since my dealing with databases is generally sparadic my memory for connection strings is horrible so I use this site:
http://www.connectionstrings.com/

It has come in very handy in the past.

I actually do have an OO wrapper for ODBC connections in C++ but that d@mn string still has to get built somehow. If there were two things I could have for dealing with databases it would be an querybuilder class and a connectionstring building class. I guess I'll just have to sit down and write them when I get a chance. Because the ODBC API is in C not C++ it's no kind of OO at all. But it's easily wrapped luckily.

# William said on January 3, 2005 9:06 PM:

Hey Bill/Andy,

In addition to connectionstrings.com, you could always create a .udl file on your desktop, double click it, and then open it in notepad for connectionstring goodness.

But in addition to sb.Encrypt = true as shown above, there's another hella neat way to protect connection strings in your ADO.NET 2.0 applications. The technique will work in November CTP onwards (doesn't work in Beta1) and is described here -- http://dotnetjunkies.com/WebLog/sahilmalik/archive/2004/12/20/37936.aspx

- Sahil

# William said on January 3, 2005 9:18 PM:

I've written my own such utility - and it's definitely not a Must have feature - but it's cool - definitley makes things easy - without connectionstrings.com I would have been hating life many times over.

# William said on January 4, 2005 10:33 AM:

Is there anything in ASP.NET and ADO.NET 2.0 that we couldn't, or haven't already, written ourselves?

# William said on January 4, 2005 10:38 AM:

I guess theoretically we could have written it all ourselves but take for instance MARS - that's a new feature that would be difficult to implement. moreover a lot of the classes are sealed so you can't augment them - you'd be forced to rewrite them from scratch. I'm not sure how you'd write the resultset for SqlMobile -

Yes, basically there's a lot of stuff that is in there that for all practical purposes - we couldn't have written. Technically we probably could have but there's no way you'd want to in most instances.

# William said on January 4, 2005 1:31 PM:

True, although from what I've briefly read about MARS it doesn't sound like it will be for everyday use, or perform well, and it will only work with the SqlClient talking to Sql Server. Mostly I was thinking about the object factory functionality. Although I'm looking forward to the ObjectDataSource.

Don't get me started on the sealed classes in the framework.

# William said on April 20, 2005 4:13 PM:

Hi !
Just a bit help please !
With VB .net 2005 Beta1 I was using this to load my main form:
Dim settings As Configuration.SettingsProviderCollection = System.Configuration.ConfigurationSettings.ConnectionString

and

Dim setting As SqlConnectionStringBuilder = settings.Item("VBXMP3.MySettings.Connection")

With the Beta2 I got a: Connectionstring is not a member of configurationSettings.

have you an idea to workaround this issue ?
Thanx a lot !


# Blog By Bob said on December 27, 2006 9:13 PM:

A coworker of mine saw my magically cool connection string parsing routine the other day and quickly

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