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 :
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;
}
}
}