Monday, July 14, 2008

C# Can't Connect to MSSQL Express: File in use by another process

Ok, I created MSSQL express database in SQL Server 2005 Express.

Now I opened C# 2005 Express and opened my project and tried to connect to the database.

FIRST OF ALL:

Why oh WHY aren't all the .SQLEXPRESS databases sitting there in an easy dropdown list? You have to hunt them down like wounded gazelles on the serengeti.

SECOND:

After hunting down the file (c:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\AFIDatabase.mdf) and going thru this sequence in the C# Express menus...

Data - Add New Data Source - Database

New Connection - Microsoft SQL Server Database File

Browse - "c:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\AFIDatabase.mdf"

Test Connection:

I get this...

---------------------------

Microsoft Visual C# 2008 Express Edition

---------------------------

Unable to open the physical file "C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\AFIDatabase.mdf". Operating system error 32: "32(The process cannot access the file because it is being used by another process.)".

An attempt to attach an auto-named database for file C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\AFIDatabase.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

Now. BEFORE you tell me to make sure SQL Server Express is closed, please note that I already DID that, ALSO, MSSQL is supposed to be MULTIUSER, which should allow SHARING anyways.

The HELP is USELESS.

Can anyone help me?



OK. Here's what I was doing wrong. I was trying to connect to a file on the drive, not the SQL Server Express Service. Here is the correct way.

  1. Open C# 2008 Express
  2. From the menu: Data: Add New Data Source.
  3. Select the Database icon and Next>.
  4. New Connection, then Change.
  5. Select Microsoft SQL Server Database File and OK.



How to Auto-generate Order Line Item numbers for bulk uploads

 I had a problem where I had 17000 line items to insert into 9000 orders. The system required line item numbers, preferably numbered 1throug...