none
mdf wont store data RRS feed

  • Question

  •  

    Hi,

     

    I'm kind of new to mdf databases, and so I ran into a problem. I used to have my database in SQL Server 2005 and thought it would be easier to deploy my solution if I used a db file in the solution, so I added an mdf file. I would also like to hear if deployment does in fact get easier.

    I then completely recreated the database structure in the mdf file. Furthermore I'm creating this C# application in visual Studio 2008 and I'm using LINQ to connect to the database.

     

    My problem is this:

    When I'm debugging I store my data into the mdf file (at least I think I'm doing that). Once I request the data in the solution it's still there. During debugging when I request to see the table data from the server explorer, it shows NULL in every table en column (I don't know if this is supposed to happen but refreshing or waiting till after debugging won't work either, there's just no data).

    When I stop debugging and edit something in the source or wait long enough and restart debugging all the previously inserted data is gone. (Note: the data seems to be occasionally stored somewhere, because sometimes the data IS still there when I run debugging a second time).

    I must mention my code worked perfectly with the SQL Server 2005 database even when I deployed it.

     

    Is there something I'm missing, a setting maybe. I though maybe there's a setting that empties the database file automatically to keep the solution a reasonable size. I'm at a loss at what the problem could be, please help.

     

    Thanks,

     

    Yoni

    Monday, March 31, 2008 9:03 AM

Answers

  • If you have added the file to your solution right click it and look at the properties - change the 'Build Action' to 'None' this will stop it being added to your output. In your connection string access it from the file location. Use a config file to store the database location. Add the file manually to your installer.

    Monday, March 31, 2008 10:17 AM

All replies

  • I bet the mdf file is getting copied to the bin folder when you compile and run - so it's always getting overwritten. If this is the case put it somewhere else and don't copy it to the output folder (slution setting)

    Monday, March 31, 2008 9:18 AM
  • I think you might be right g#. To add some information to my problem I've deployed it and when I use a function that accesses the database I get the following error:

     

    System.Data.SqlClient.SqlException: An attempt to attach an auto-named database for file C:\Program Files\Y.Chamla @Home\Dvd Database 1.0.2\Data\DVD_DB.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

     

    Could you give small explanation of how to detach the mdf from my solution (in the solution settings) and place it somewhere else. I still need to access the mdf file after detaching so I can add the tables from this database to my LINQ dbml file.

     

    Thanks,

     

    Yoni

     

    Monday, March 31, 2008 10:09 AM
  • If you have added the file to your solution right click it and look at the properties - change the 'Build Action' to 'None' this will stop it being added to your output. In your connection string access it from the file location. Use a config file to store the database location. Add the file manually to your installer.

    Monday, March 31, 2008 10:17 AM
  •  

    Thanks that worked great.

     

    Yoni

    Monday, March 31, 2008 10:32 AM