Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Server .mdf file being used by another process ... 1

Status
Not open for further replies.

vodkasoda

Programmer
May 6, 2003
4
Hi, I am new here and new to C#, SQL Server & Visual Studio, though I have 30 years of mainframe programming experience ... hopefully somebody can help ...

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, (1) is my understanding of the way the database is copied and maintained correct, and if so (or if not !!) (2) 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 :
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++;
            //MessageBox.Show("myCount = " + myCount.ToString());
            //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;
        }
    }
}
 
I have never tried to manage a db from Visual Studio but it sounds like your data bases are still attached to SQL Server.

If want to delete them in SQL server you can DROP DATABASE or DETACH the files then delete or move them manually. Once moved you would have to reattach them to use them in SQL Server

Simi
 
It definitely seems to be the case that VS2010 gets its knickers in a twist over a DB created inside the Project. I have now created the DB in MS SQL Server Management Studio and added it as an Existing item and it works just fine ... phew, at last !!!
 
How do I close this thread and/or mark it as closed ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top