B
bob_jeffcoat
Hi,
I'm trying to:
1) create an SQL database in C#
2) open a connection to it
3) close the connection
4) drop the database
If I don't do parts 2) and 3) it works, I can create and drop a
database. If I do parts 2) and 3) the drop fails as the database is in
use. Why? Can anyone help?
Here's my test:
[Test]
public void DeleteTest()
{
SqlCommand cmd;
string sql;
string databaseName = "TEST";
SqlConnection sqlConnection = new SqlConnection(
"Server=Server1;Integrated Security=True;User
Instance=False");
sqlConnection.Open();
sql = "CREATE DATABASE " + databaseName;
cmd = new SqlCommand(sql, sqlConnection);
cmd.ExecuteNonQuery();
SqlConnection databaseConnection = new SqlConnection(
"Server=Server1;Initial Catalog=" + databaseName +
";Integrated Security=True;User Instance=False");
databaseConnection.Open();
databaseConnection.Close();
System.Threading.Thread.Sleep(10000);
sql = "DROP DATABASE " + databaseName;
cmd = new SqlCommand(sql, sqlConnection);
cmd.ExecuteNonQuery();
}
Any help would be greatly appriciated.
Thanks,
Bob
I'm trying to:
1) create an SQL database in C#
2) open a connection to it
3) close the connection
4) drop the database
If I don't do parts 2) and 3) it works, I can create and drop a
database. If I do parts 2) and 3) the drop fails as the database is in
use. Why? Can anyone help?
Here's my test:
[Test]
public void DeleteTest()
{
SqlCommand cmd;
string sql;
string databaseName = "TEST";
SqlConnection sqlConnection = new SqlConnection(
"Server=Server1;Integrated Security=True;User
Instance=False");
sqlConnection.Open();
sql = "CREATE DATABASE " + databaseName;
cmd = new SqlCommand(sql, sqlConnection);
cmd.ExecuteNonQuery();
SqlConnection databaseConnection = new SqlConnection(
"Server=Server1;Initial Catalog=" + databaseName +
";Integrated Security=True;User Instance=False");
databaseConnection.Open();
databaseConnection.Close();
System.Threading.Thread.Sleep(10000);
sql = "DROP DATABASE " + databaseName;
cmd = new SqlCommand(sql, sqlConnection);
cmd.ExecuteNonQuery();
}
Any help would be greatly appriciated.
Thanks,
Bob