Connecting ASP.NET to a Named SQL Server Instance

by Nate on July 19, 2006

This may be obvious to most, but being new to ASP.NET – and relatively new to programming in general – I found it difficult to connect my ASP.NET application to a SQL Server instance. Looking at the available documentation, connecting should be easy. You simply create a connectionStrings element in your application’s web.config file and point it to the right server and database, right?

Well, yeah, but only after clearing out the default inherited connection settings. Out of the box, ASP.NET points to a SQL Server 2005 instance named, of course, SQLEXPRESS. If you don’t have a SQLEXPRESS instance running on your server, you have to either change the machine.config file located at

SystemDrive\WINDOWS\MICROSOFT.NET\FrameworkXX\vX.X.XXXXX\CONFIG

or clear out the inherited connection settings using the clear element and then add in your own connection settings to the web.config file in your web application. Here’s the code to clear out the inherited connection settings and connect your application to another – local – SQL Server database:

name=”LocalSqlServer”
connectionString=”data source=.\YourSQLServerInstance;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|YourDatabase.mdf;User Instance=true”
providerName=”System.Data.SqlClient”
/>

After you specify the correct connection settings, you should be able to use your SQL Server database to store your application’s data.

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post:

Next post: