Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 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 :

C#
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;
    }
}
}
Posted

Personally, I would not do it this way, it leaves you open to all sorts of issues. I'd just put the DB there, and leave Visual Studio out of it, I'd manually keep a master copy in another folder and copy to the debug folder for testing
 
Share this answer
 
Comments
Gary Heath 13-Feb-12 13:26pm    
The problem I have, Christian, is that I am brand new to SQL Server, C# & Visual Studio, so am trying to figure things like that out !!! If your way is a better way of doing things then I'd like to get to that position, but I keep running into these stupid little problems that are just preventing me from doing anything !!!
Christian Graus 13-Feb-12 13:45pm    
No-one should be new to all those things and learn them at once. It just makes no sense. If you're creating a DB to deploy to an end user who does not have SQL Server installed, then I'd suggest SQLite. It allows multuple users. If you're not, then there's no need for the file to be in your directory. Either way, you should learn C# first, then learn SQL after that. Or use Entity Framework and learn LINQ instead, although SQL is probably easier, a lot of the time.
Gary Heath 13-Feb-12 17:14pm    
It's not ideal, no, but needs must ...
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 !!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900