sql exception when transferring project from usb to c:\

331 Views Asked by At

I'm working on a C# windows program with Visual Studio 2008. Usually, I work from school, directly on my usb drive. But when I copy the folder on my hard drive at home, an sql exception is unhandled whenever I try to write to the database. it is unhandled at the conn.Open(); line. here's the exception unhandled

Database 'L:\system\project\the_project\the_project\bin\Debug\PatientMonitoringDatabase.mdf' already exists. Choose a different database name. Cannot attach the file 'C:\Documents and Settings\Administrator\My Documents\system\project\the_project\the_project\bin\Debug\PatientMonitoringDatabase.mdf' as database 'PatientMonitoringDatabase'.

it's weird, because my connection string says |DataDirectory|, so it should work on any drive... here's my connection string:

string connStr = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\PatientMonitoringDatabase.mdf; " +
                "Initial Catalog=PatientMonitoringDatabase; " +
                "Integrated Security=True";
3

There are 3 best solutions below

3
On BEST ANSWER

I think the complaint here is that your local SQL Server Express install already has a PatientMonitoringDatabase attached and running.

Connect to localhost with SQL Server Management Studio Express (if not installed, download here) and remove/detach the existing PatientMonitoringDatabase database. Whether it's a persistent database or only active within a running application, you can't have 2 databases with the same name at the same time attached to a SQL Server instance.

4
On

The second exception you get because the SQL Server Express instance doesn't have permission to open the database file. What are the permissions on the file, and what is SQL Server running under?

0
On

problem solved. got lucky on that one. I got a hint by browsing microsoft forums. The hint led me to open my "SQL Server Configuration Manager", and I had to do something with "SQL Server (SQLEXPRESS)". So I double clicked on it, and changed the "Built-in Account" from "Network Service" to "Local System". and now it works.... I got lucky i guess...

but, you do have to detach the database. So for that, thx Nick Craver