I wrote a very small C # program that uses a very small SQL Server database, solely for some training and testing purposes. The database is used in this new project and nowhere else. However, I am having problems starting Debugs, where the program will not start because the database is "being used by another process."
If I reboot my machine, it will work again, and then after several test starts, I will get the same problem again.
I have found many, many similar problems reported all over the Internet, but I cannot find a definitive answer to the question of how to solve this problem. Firstly, how do I know that a “different process” uses my .mdf and .ldf files? Then, how can I get these files released and not held in order to stop this from time to time after time ?!
I am new to VS2010, SQL Server and C #, so please be descriptive enough in any answers you give me !!!
This is my code, as you can see, you cannot get anything more basic, of course, I should not face many problems !!!
namespace MySqlTest
{
public partial class Form1 : Form
{
SqlConnection myDB = new SqlConnection(@"Data Source=MEDESKTOP;AttachDbFilename=|DataDirectory|\SqlTestDB.mdf;Initial Catalog=MySqlDB;Integrated Security=True");
SqlDataAdapter myDA = new SqlDataAdapter();
SqlCommand mySqlCmd = new SqlCommand();
string mySQLcmd;
int myCount;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("myDB state = " + myDB.State.ToString());
myDB.Open();
MessageBox.Show("myDB state = " + myDB.State.ToString());
}
private void button2_Click(object sender, EventArgs e)
{
myCount++;
MessageBox.Show("myCount = " + myCount.ToString());
mySqlCmd.Connection = myDB;
mySqlCmd.CommandText = "INSERT INTO Parent(ParentName) Values(myCount)";
myDA = new SqlDataAdapter(mySqlCmd);
mySqlCmd.ExecuteNonQuery();
}
private void button3_Click(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
}
private void button5_Click(object sender, EventArgs e)
{
}
private void button6_Click(object sender, EventArgs e)
{
MessageBox.Show("myDB state = " + myDB.State.ToString());
myDB.Close();
MessageBox.Show("myDB state = " + myDB.State.ToString());
}
private void button7_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
source
share