Checking for open db

J

John

Hi

In my app I am opening a db connection then running a set of queries and at
the end closing the db connection. Once in a while a query fails to run
saying database connection is closed. How can I ensure that db connection is
confirmed open before running a query? Will

dbconn.close
dbconn.open

before running a query do the trick or do I need to make more comprehensive
checks?

Thanks

Regards
 
G

Guest

before running a query do the trick or do I need to make more
comprehensive checks?

Check the state of the connection.

I believe connection.state will give you the connection state. Just make
sure the connection is "Open".

You can also wrap your call in a try/catch - if it fails, repeat the call.

Lastly you may want to wrap your SQL in a transaction to prevent orphaned
records from being inserted.
 
J

John

Hi

Thanks. Not sure about the last point though, How do I implement
transactions? Any link etc would be appreciated.

Thanks

Regards
 
C

Cor Ligthert[MVP]

John,

In fact is this a failure in your program, you control the program, so
somewhere you have not closed or opened it. I try to be very secure always
to have my database actions in small methods, wherin is the open of the db
as the close of the db.

dbopen and dbclose are C (SQL) commands

dbconnection.open and dbconnection.close methods of the mustinherit
Connection class.

Cor
 
R

rowe_newsgroups

Hi

Thanks. Not sure about the last point though, How do I implement
transactions? Any link etc would be appreciated.

Thanks

Regards

You can search the help for DbTransaction (replace "Db" with your
provider (Sql/OleDb) for more specific help) or go to msdn.com and
search there. Both will bring up Microsoft's documentation for this.

Also, I believe it was Bill Vaughn that said that connection.State
will only tell you what it "thinks" it's state is - so this could
return a state of Open even if the connection has been lost - so make
sure you use a try.. catch just in case.

Thanks,

Seth Rowe
 
R

rowe_newsgroups

dbopen and dbclose are C (SQL) commands

Huh?

I think you misread something...

Thanks,

Seth Rowe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top