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

Database question
baldingfatty
n y n--d8bn pr95y6
maxcredible
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;
}
Sayeed
Disaia
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 )
marlin1
Vegethalia
SSEGARANE
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
Paolo Ponzano
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.
Zane S.