Database question

I am writing a program that will allow you to create new databases on a SQL server. However, I am getting an error that says the database 'MODEL' is being used, and we can't get an exclusive lock on it. I have no problem creating a new database using other programs unless my program is open. So I am pretty sure that I am not closing a connection to the database. However, I have checked through my code, and have found a connection.Close() everywhere there is an Open(). Could there be something I am missing that would cause this error?
[542 byte] By [codefund.com] at [2007-12-16]
# 1
Does this error occur only when the program is open, only when it is closed, or both?
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2
Close releases the connection back to the connection pool, if connection pooling is enabled (and it probably is). You might want to try calling YourConnection.Dispose, if you're really done with the Connection object and don't need to reopen it.
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3
Great point Ken. I was going to mention that but didn't want to "jump the gun".
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 4
Thanks for the replies - I tried calling dispose on my connection object, still getting the same error.

Yes, this error only occurs when my program is running. I can create databases using other programs when my program is closed just fine. But when I have my program running, I can't create a database in either my program or another. There shouldn't be any connections open in my program except for at startup, and during any transactions initiated by the user. In all cases, I close the connection as soon as I am done with them.

I am using SqlDataReader in a few places, and I close it when I am done, do I need to dispose of that too (how would you do that without a Dispose() method?)?

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 5
How are you use the SqlDataReader? (There isn't a Dispose method on a SqlDataReader, so you needn't worry about disposing it.)

Remember that if you pass the SqlDataReader's constructor a closed Connection object, it will open the connection, get the data, then close it. If you pass it an open Connection, it remains open once you're done. That is, if you pass a closed Connection object, you needn't worry about closing it, but you should dispose it if you no longer need it.

I doubt this information helps solve the problem, but it's good stuff to remember.

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 6
Thanks for the info - that is good stuff to be aware of. I am opening the connection, passing it to the datareader and then closing and disposing of it myself.
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 7
I'm lazy, generally, and pass a connection string to the SqlDataAdapter's constructor. That way, I don't have to worry about opening or closing the connection. Up to you...
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 8
Well, I'm still having the same problem: "Exclusive access could not be obtained because the database is in use." I have (I think) narrowed it down to two functions that cause the problem. If I don't run either of the two functions, everything works fine, but if one or both is run I have the problems. The two functions are almost identical. They take in a string for the name of a database, and will return an ArrayList of the Tables in that database, or the Views in that database (depending on which function is called) Here is the code for the GetTables function:

public ArrayList GetTables(string database)
{
string sql = "SELECT TABLE_NAME, TABLE_TYPE FROM INFORMATION_SCHEMA.TABLES" +
" WHERE (TABLE_TYPE = \'BASE TABLE\')" +
" ORDER BY TABLE_NAME";

SqlConnection conn = null;
SqlDataReader reader = null;
ArrayList tableList = new ArrayList();

try
{
//set connection
conn = new SqlConnection(connection.GetConnectionForDatabase(database));

SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
reader = cmd.ExecuteReader();

while(reader.Read())
{
tableList.Add(reader.GetString(0));
}
}
catch(SqlException sqlErr)
{
throw sqlErr;
}
catch(Exception err)
{
throw err;
}
finally
{
if(reader != null)
{
reader.Close();
reader = null;
}

conn.Close();
conn.Dispose();
}

return tableList;
}

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 9
Hmmm... I tried your code, followed immediately by an attempt to read data from the same database I just checked out with your function. No problems at all. I don't know what you're doing, but my code looked like this (it's stupid code, but I think it tests something like what you're doing):
ArrayList x = GetTables("Northwind");

SqlConnection cnn = new SqlConnection("Database=Northwind;Integrated Security=True;Server=.");
SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", cnn);
DataSet ds = new DataSet();
cnn.Open();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(ds);
cnn.Close();
cnn.Dispose();
I had no problems. Can you try something like this and see if you still fail?

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 10
Regular queries work just fine, but if I try to create a new one, or try to restore a database then it will give me exclusive lock errors.
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...