PC Review Forums Newsgroups Microsoft DotNet Microsoft ADO .NET Pb with DataAdapter.Fill

Reply

Pb with DataAdapter.Fill

 
Thread Tools Rate Thread
Old 07-02-2006, 08:40 AM   #1
=?Utf-8?B?YnA=?=
Guest
 
Posts: n/a
Default Pb with DataAdapter.Fill


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
  Reply With Quote
Old 08-02-2006, 03:46 AM   #2
=?Utf-8?B?VmFsIE1henVyIChNVlAp?=
Guest
 
Posts: n/a
Default RE: Pb with DataAdapter.Fill

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

--
Val Mazur
Microsoft MVP
http://xport.mvps.org


"bp" wrote:

> 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

  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off