I have problem when I do connection to Service based Database, to mdf file after I opened the connection I cant closed it.
That is my Code: Create 2 buttons and 1 OpenDialogFile
private void button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(connectionString(db.mdf));
conn.Open();
conn.Close();
}
private void button2_Click(object sender, EventArgs e) {
openFileDialog1.Filter = "Database File|*.mdf|All files |*.*";
openFileDialog1.DefaultExt = "mdf";
openFileDialog1.FileName = "db.mdf";
if (openFileDialog1.ShowDialog() == DialogResult.Cancel)
{
return;
}
textbox1.Text = openFileDialog1.FileName;
}
After click on the first button the file in used and cannot do the second button
My Problem: i got connection string i can get the DataBased connected; but after i do it such as: Conn.Open(); no metter what i do Conn.close(); or anything else the file, the DB.MDF file is in used until i closed the whole project... it mean if i wanna open it and closed it i need open and close the project...
Edit: Try even that:
using (SqlConnection connection = new SqlConnection(ConnectString))
{
connection.Open();
}
still not worked