Board index » delphi » best practice for ado query and exception handling

best practice for ado query and exception handling

hi,
whats the best practice for the procedure below to query table and handle
exceptions in a senseful way.
thanks

procedure MyQuery(sql: string);
begin
 try
  connection.open;
 except
  connection.close;
  // handle connection exception
  Exit;
 end;

 try
  try
   query.close
   query.SQL.Text := sql;
   query.open
  except
   // handle query exception.
  end;
 finally
  query.close;
  connection.close;
 end;
end;

 

Re:best practice for ado query and exception handling


Quote
>  try
>   query.close
>   query.SQL.Text := sql;
>   query.open
>  except
>   // handle query exception.
>  end;

This is basically all you need.
If the query has an error opening it will not have opened and there is no need
to close it.  
closing your Connection will only slow you down the next time you have to use
the connection not to mention closing any other dataset connected to it.
When you have an error look at the TadoConnection.errors collection to find out
what the error was
--
Brian Bushay (TeamB)
Bbus...@NMPLS.com

Re:best practice for ado query and exception handling


but i need to connect to several databases for some testing. also if i want
to do some processing on the huge resultset of a query and then need to
close it, isn't it suitable to close?

Quote
>  try
>   query.close
>   query.SQL.Text := sql;
>   query.open
>  except
>   // handle query exception.
>  end;

"Brian Bushay TeamB" <BBus...@Nmpls.com> wrote in message
news:lh264v0iptmd0pum7g59t0dmjpge3kmpc6@4ax.com...
Quote
> >  try
> >   query.close
> >   query.SQL.Text := sql;
> >   query.open
> >  except
> >   // handle query exception.
> >  end;

> This is basically all you need.
> If the query has an error opening it will not have opened and there is no
need
> to close it.
> closing your Connection will only slow you down the next time you have to
use
> the connection not to mention closing any other dataset connected to it.
> When you have an error look at the TadoConnection.errors collection to
find out
> what the error was
> --
> Brian Bushay (TeamB)
> Bbus...@NMPLS.com

Re:best practice for ado query and exception handling


Quote
>but i need to connect to several databases for some testing.

Then I would close the connection just before you change the connection string

Quote
>also if i want
>to do some processing on the huge resultset of a query and then need to
>close it, isn't it suitable to close?

If you have a good reason to close it then of course you can use close.
Your question seemed to be asking about a generic way of handling a query.

--
Brian Bushay (TeamB)
Bbus...@NMPLS.com

Other Threads