How is the debug version of a SQL Server database maintained ?

  • I am getting the Errors

    "Unable to copy file "j:\users\gary\documents\visual studio 2010\Projects\MyApp01\MyApp01\MyApp01.mdf" to "bin\Debug\MyApp01.mdf". The process cannot access the file 'bin\Debug\MyApp01.mdf' because it is being used by another process."

    and

    "Unable to delete file "j:\users\gary\documents\visual studio 2010\Projects\MyApp01\MyApp01\bin\Debug\MyApp01.mdf". The process cannot access the file 'j:\users\gary\documents\visual studio 2010\Projects\MyApp01\MyApp01\bin\Debug\MyApp01.mdf' because it is being used by another process."

    I have been working on getting this problem fixed for almost a week now, but still cannot fathom out what is wrong.

    Today I have created a very basic new Project called MyApp01 & connected a new 2 Column Database of the same name, and I edited the Properties to change the "Copy to Output Directory" status from "Copy Always" to "Copy if Newer". My understanding of this is that the database that is stored in the MyApp01 Folder creates a test version of it in the MyApp01\bin\Debug\ folder the first time it is accessed and then is only overwritten if the Database layout is changed. Is that correct ? If so, why am I getting these errors that seem to insinuate that the system is trying to replace the \bin\Debug\ version of the database ?!?

    I ran the App for the first time & it seemed to work just fine, my database was updated with three records, these displayed in Form2 and I Quit out of it successfully. However, back in VS2010 I used the Server Explorer to Show Table Data and this shows the Database as empty, so I believe that insinuates it is looking at the version in the MyApp01 folder & not the version in the MyApp01\bin\Debug\ folder. Therefore I ran the program again and this is where I get these errors.

    So, again, is my understanding of the way the database is copied and maintained correct, and if so (or if not !!) why am I getting these errors telling me that the system is trying to replace the \bin\Debug\ version of the database ?!?

    This is my code :

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Linq;

    using System.Text;

    using System.Windows.Forms;

    using System.Data.SqlClient;

    namespace MyApp01

    {

    public partial class Form1 : Form

    {

    int myCount;

    string myDBlocation = @"Data Source=MEDESKTOP;AttachDbFilename=|DataDirectory|\MyApp01.mdf;Integrated Security=True;User Instance=False";

    public Form1()

    {

    InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)

    {

    myCount++;

    //Insert Record Into SQL File

    myDB_Insert();

    }

    private void button2_Click(object sender, EventArgs e)

    {

    Form2 form2 = new Form2();

    form2.Show();

    }

    private void button3_Click(object sender, EventArgs e)

    {

    //Quit

    myDB_Close();

    this.Close();

    }

    void myDB_Insert()

    {

    using (SqlConnection myDB = new SqlConnection(myDBlocation))

    using (SqlCommand mySqlCmd = myDB.CreateCommand())

    {

    mySqlCmd.CommandText = "INSERT INTO MyAppTbl(MyData) VALUES(@MyValue)";

    mySqlCmd.Parameters.AddWithValue("@MyValue", myCount);

    myDB.Open();

    MessageBox.Show("State = " + myDB.State);

    mySqlCmd.ExecuteNonQuery();

    myDB.Close();

    MessageBox.Show("State = " + myDB.State);

    }

    return;

    }

    void myDB_Close()

    {

    using (SqlConnection myDB = new SqlConnection(myDBlocation))

    using (SqlCommand mySqlCmd = new SqlCommand())

    {

    myDB.Close();

    }

    return;

    }

    }

    }

  • Hi Gary,

    You question is going to be addressed much quicker on an asp.net forum. This is not an SQL Server issue really, and so you are not likely to get too much help here.

    Jared
    CE - Microsoft

  • It's not SQL Server ?!? This is some learning curve I've got myself into !!!

    OK, thanks, I'll obviously leave the question open, there are so many similar questions on the Internet that somebody must know what's causing it, but I'll try an Asp.net forum as well ... thanks.

    Gaz

  • It certainly seems that VS2010 gets its knickers in a twist when you add a New Database within the Project and then use the Debug routine.

    I have resolved this now by using a New Database that I created in MS SQL Server Management Studio and I added it to the VS2010 C# Project as an Existing Item, rather than a New Item, and it works just fine ... phew, at last !!!

    Do I have to close this thread myself or mark it as resolved ? If so I can't see how to do so ...

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply