views:

25

answers:

2

Hi!

A while ago, I wrote a web-based guestbook application that wrote it's own database.

My code was very amateurish, but, as it was my very first publication, I was very happy with it. Only about a month after I'd published it did I realize I'd made a huge mistake in the code.

I've only ever connected to a specific named instance of SQL Server, and it occurred to me that, if the SQL Server instance has a different name than the one I specified, it wouldn't work.

So, since my users will probably not know what the name of the instance of SQL Server that's running is, I thought adding a field where the user can specify it would help if they do, but what if they don't? My answer was to get the local instance, regardless of name.

I tried Data Source=.\local;, Data Source=.; and other variants, but nothing worked.

Any ideas?

A: 

A non-named instance is called "default instance" and can be accessed using "Data Source=(local);...".

Anton Gogolev
I don't think I tried that one, thanks :) Only issue is I'm getting `Could not open a connection to SQL Server`
Logan Young
A: 

The only way of knowing which instances are running is by querying the SQL Browser Service, if it's running. I guess most of your users are running SQL Server Express? If so, the service is called local\SQLEXPRESS. If not, it's local or they've named it themselves. So, I would try local\SQLEXPRESS first, then local. If none works, just throw an exception and ask the user to add it themselves.

Vidar Nordnes
The big problem is that I can never know anything about their system (what version of SQL Server they're running included).As I said, I thought of adding a field for the user to type in the name of his/her SQL Server instance, but what happens if they don't know it?
Logan Young
If the user doesn't know it's either SQLExpress or default. At least in 99.9% of the cases :-)
Vidar Nordnes