Programming Practicies

  • Thread starter Thread starter Doug Durrett
  • Start date Start date
D

Doug Durrett

I'm an old VB6 guy and in VB6 I could Open up a DataConnection object as
PUBLIC and use it anywhere in the application.

What do I do in C# or what is the "right" way. should I be creating a new
connection EVERY time I need something from the Database?

Just basics.... Thanks In Advance.
Doug
 
Ive found the new database wizard is excellent for making a database, I
cant find it with the version Im using on C#, but it lets you specify the
type and identify the database *.mdb and test it, then it writes some code
for you.

What I found was that opening the main database, then copying to a temp
database, closing the main, then you can access the temp db.

As to the connection staying open? Im interested to learn the answer, I try
to close it as a rule for each access, but it might depend on the complexity
of the application. If its bank data? One would want more safeguards in
place obviously, and ways to track each transaction.
 
For the most part opening and closing the connection to a database is
preferable to leaving it open for extended periods of time. Basically
it comes down to how frequently you will need to use the connection vs
many connections to your database you want to take up. What you have to
keep in mind is that most databases have some form of maximum user
connections (whether it is due to licensing or locking) and when your
application opens one up when it starts and doesn't close it until the
application is closed then that is one less connection that is available
for others to use.

Imagine that you have five connections available to your database and
ten database users... if you leave the connection open for your
application you can only get half the users into the system before it
locks the others out. At that point you either have to redesign or you
have to buy more licenses. However if you open and close connections
only as needed you'll find that five licenses is more than enough to
handle your users (unless the application constantly hammers the
database) and will actually handle a lot more users as well.

This is my 2 cents.

Have A Better One!

John M Deal, MCP
Necessity Software
 
Connection pooling means you really don't have to worry about constantly
closing and re-opening connections. On the other hand if you are writing,
say, a batch processing utility that will hit the database continuously for
an hour, then it probably is an advantage to hold open a single connection
for the whole run.

It all depends on the access pattern -- if you're hitting the DB hard and
continuous, then holding a connection open for at least that operation
probably makes sense. But for typical CRUD operations, it's best to rely on
connection pooling.

--Bob
 
Back
Top