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, and I run it from 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";

Someone told me to connect to localhost with SQL Server Management Studio Express, 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.

So I did that, and now it gives me:

Directory lookup for the file "C:\Documents and Settings\Administrator\My Documents\system\project\the_project\the_project\bin\Debug\PatientMonitoringDatabase.mdf" failed with the operating system error 5(Access is denied.). Cannot attach the file 'C:\Documents and Settings\Administrator\My Documents\system\project\the_project\the_project\bin\Debug\PatientMonitoringDatabase.mdf' as database 'PatientMonitoringDatabase'

I checked the files' properties, and I have allow for everyone. Does anyone know what's going on here?

Recommended Answers

All 6 Replies

>Database 'L:\system\project\the_project\the_project\bin\Debug\PatientMonitoringDatabase.mdf' already exists.

SQL server already have a PatientMonitoringDatabase database. Change the Logical name.

oh I forgot to tell you guys, i fixed the problem. I had to change a setting in my SQL server configuration manager.

>oh I forgot to tell you guys, i fixed the problem.

No problem, glad I could help. Please mark this thread as solved.

how do i mark it as solved?

>how do i mark it as solved?

You will find Mark as Solved link at the end of last post.

I had a similar problem, although in different scenario. (tried to connect to 2 different databases) and kept getting this error.
Solution: in connection string put the name of your database file (without extension) as Initial Catalog

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.