Pb with DataAdapter.Fill

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

I'm trying to make a component to manage database access. One task of this
component is to create a database if it does not exist. To minimize queries's
count, I want to do database creation when DataAdapter.Fill fails (and if the
database doesn't exist). So I wrote the following code :

try
{
dataAdapter.Fill(ds);
}
catch
{
// Use a connection on master database to create a database
connectionMaster.Open();
commandMaster.ExecuteNonQuery(); // Create database query
connectionMaster.Close();

dataAdapter.Fill(ds);
}

But it fails on the second DataAdapter.Fill : "Can't open connection"

When I wrote the following code, it works :

connectionMaster.Open();
commandMaster.ExecuteNonQuery();
connectionMaster.Close();

dataAdapter.Fill(ds);

It seems that some resources have been blocked by the first
DataAdapter.Fill. How can I make my first code work ?

Thanks for responses.

bp
 
Hi,

I believe if database does not exist, then you suppose to get an exception
when you try to open database. You do not need to call Fill of the
DataAdapter to do this. Anyway, if you need to call Fill for some reason,
then it is better to call Dispose method of the DataAdapter after you got an
exception, set variable to null an then continue
 
Back
Top