Hi,
i have a problem dropping a database?
I have a WinForm and i can create DB, create the Tables and drop the DB via buttons.
When i start myProgram it's no problem to drop the Database.
Then i can create the database without problems.
The tables also without problems.
Now if i try to drop my whole DB, i get the error message:
SqlException Handle :System.Data.SqlClient.SqlException: Cannot drop database "XtmsDb" because it is currently in use.
The code for dropping db:
public void deleteProjectDB()
{
conn.ConnectionString = "Data Source=TURM21;Initial Catalog=master;Integrated Security=SSPI;";
sqlStr = "Drop database XTmsDB";
try
{
System.Console.WriteLine("Opening Connection...");
conn.Open();
System.Console.WriteLine("Connection opened!!!");
SqlCommand cmd = new SqlCommand(sqlStr, conn);
cmd.ExecuteNonQuery();
System.Console.WriteLine("Database dropped!!!");
}
catch (SqlException dropEx)
{
System.Console.WriteLine("SqlException Handle :{0}", dropEx.ToString());
}
finally
{
conn.Close();
System.Console.WriteLine("Connection closed!!!");
}
}
The code for creating db:
public void createProjectDB()
{
conn.ConnectionString = "Data Source=TURM21;Initial Catalog=master;Integrated Security=SSPI;";
sqlStr = projectDBStrings.getDBCreateString();
try
{
System.Console.WriteLine("Opening Connection...");
conn.Open();
System.Console.WriteLine("Connection opened!!!");
SqlCommand cmd = new SqlCommand(sqlStr, conn);
cmd.ExecuteNonQuery();
System.Console.WriteLine("Database created!!!");
}
catch (Exception)
{
System.Console.WriteLine("Could not establish Connection!");
}
finally
{
conn.Close();
System.Console.WriteLine("Connection closed!!!");
}
}
The code for creating Tables:
public void createProjectTables()
{
conn.ConnectionString = "Data Source=TURM21;Initial Catalog=XtmsDb;Integrated Security=SSPI;";
ArrayList createList = projectDBStrings.fillProjectSchema();
SqlTransaction tx;
SqlCommand cmd = new SqlCommand("", conn);
IEnumerator createListEnum = createList.GetEnumerator();
try
{
System.Console.WriteLine("Opening Connection...");
conn.Open();
System.Console.WriteLine("Connection opened!!!");
try
{
while (createListEnum.MoveNext())
{
sqlStr = (String)createListEnum.Current.ToString();
tx = conn.BeginTransaction();
cmd.CommandText = sqlStr;
cmd.Transaction = tx;
cmd.ExecuteNonQuery();
tx.Commit();
}
System.Console.WriteLine("Schema created!!!");
}
catch (SqlException deleteEx)
{
System.Console.WriteLine("SqlException Handle :{0}", deleteEx.ToString());
}
}
catch (SqlException connectionEx)
{
System.Console.WriteLine("SqlConnection Handle : {0}", connectionEx.ToString());
}
finally
{
conn.Close();
System.Console.WriteLine("Connection closed!!!");
}
}
Who can help me?You can't drop a database while you (or anyone) are connected to it. You will need to switch your connection to a different database before issuing your command.|||But i closed the connection. Isn't that enough. How can i change connection to other DB? SOrry, but i'm a beginner.
Greetz|||Ok i know hoe to change. But why isnt it enough to close connection?|||I'm not an application programmer, but if you close the connection how can you issue ANY command to the server?|||I like to think I'm an application programmer.
Just looking at the code... isn't Crean connected to Master when he tries to drop the db?|||yes, i'm connecting to master, so i don't know where the problem is.
I changed to another db
conn.ChangeDatabase("XTMSTEST2");
Now it works. But its not a nice solution.
Anyone knows what the problem is?|||If there are any open connections to the database you cannot drop the database and the error is self-explanatory. In general it is better to use master database whenever dropping any user datatabase.|||Yes but i switched to master in the connection string.
But I solved my Problem. U have to turn pooling off in connection String.
On master and on xtms
Connection string looks like this:
Data Source=TURM21;Initial Catalog=master;Integrated Security=SSPI;pooling false
So thats the solution. But what are the drawbacks if pooling is on false?
Really a great forum. U always get an answer. Great!!!!!!!!!!!
Greets|||Looks like you just GAVE the answer.
Thanks for posting the solution.|||U can improve the perfomance of ur application by enabling pooling.
Applications often have different users performing the same type of database access.
For example, many users might be querying the same database to get the same data. In those cases, the performance of the application can be enhanced by having the application pool, connections to the data source.
The overhead of having each user open and close a separate connection can otherwise have an adverse effect on application performance.|||U can improve the perfomance of ur application by enabling pooling.
Applications often have different users performing the same type of database access.
For example, many users might be querying the same database to get the same data. In those cases, the performance of the application can be enhanced by having the application pool, connections to the data source.
The overhead of having each user open and close a separate connection can otherwise have an adverse effect on application performance.In most cases, this is quite correct. In this case, since the pooled connection stays in a database that needs to be unused in order to be dropped, turning off the pooling for this particular operation seems to be required, not optional.
-PatP|||thx all.
Thread closed. ;)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment