views:

126

answers:

3

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";
+1  A: 

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
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
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
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
A: 

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
what do you mean by SQL server running under? like, how do I find that information?
jello
and it's funny, cause everything works fine when i run it from my usb
jello
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
funny how i don't have a security tab...
jello
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
A: 

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