0

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";
sorin
  • 161,544
  • 178
  • 535
  • 806
jello
  • 745
  • 5
  • 12
  • 21

3 Answers3

1

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.

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • how do you connect to localhost? I connected servername: "JELLO/SQLEXPRESS". aunthentication: windows authentication. I then detached PatientMonitoringDatabase, and now, it gives me another exception when I try to write to database. i quote the exception in my next comment – jello Mar 07 '10 at 19:30
  • 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'. – jello Mar 07 '10 at 19:31
  • or maybe it has something to do with the fact that an error pops up when I run SQL management studio express? "Unhandled exception has occurred in a component in your application. If you click Continue, the application will ignore this error and attempt to continue. Cannot create a stable subkey under a volatile parent key." – jello Mar 07 '10 at 19:39
0

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?

Dean Harding
  • 71,468
  • 13
  • 145
  • 180
  • what do you mean by SQL server running under? like, how do I find that information? – jello Mar 09 '10 at 04:08
  • and it's funny, cause everything works fine when i run it from my usb – jello Mar 09 '10 at 04:13
  • Check the permissions on the .mdf file (that is, right-click, select Properties and then the Security tab). The simplest solution is probably to select "Users" and then make sure the "Modify" entry is checked. – Dean Harding Mar 09 '10 at 05:52
  • ok i have the security tab now. but, I don't have a "Users". I only have "Administrator", "Administrators", and "SYSTEM". All 3 have "Modify" checked. – jello Mar 09 '10 at 17:12
0

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

jello
  • 745
  • 5
  • 12
  • 21