Public connection declaration

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi

I want to declare one single global connection for my app. Do I need the
withevents in declaration, as follows?

Public WithEvents dbConContacts As System.Data.OleDb.OleDbConnection

Thanks

Regards
 
Ignoring the question of whether or not such an approach is beneficial, if
you want to use a declarative event hookup/handler then you should use this
approach. Remember that essentially what happens when you use WithEvents is
that this field is now morphed into a property when the project is compiled.
So basically when you assign this to something, you'll get the value and the
hookup will take place. On the other hand, when you set it, the events are
unhooked, the value is assigned and then any new events are hooked back in.

If you are going to trap InfoMessage
http://www.knowdotnet.com/articles/connections.html or whatever other events
than this would certainly make things a little cleaner. I know this sounds
like a dodge of the question, but the usefulness really depends on the
implementation and what you're trying to accomplish.

If I may, what is the ultimate goal you are trying to accomplish with this
vs having a connection defined in a dataaccess layer/class for instance?
 
I am going to use data adapters, which would all be assigned this global
connection. I don't know how to implement it any better/easier. Also the
database back end is access 97. Yuck.

Regards
 
John:

Wherever your class that holds the dataadapters is though, couldn't you just
include the OleDbConnection there, store the ConnectionString globally and
go from there. You aren't instantiating the Connection globally, just
declaring it. So instead if you declared/instantiated the connection each
time you created an instance of the class, you could just use the globally
stored connection string or better yet, store the connection string in a
..Config file (stored encrypted just to be safe).
Before I recommend this though, how are the dataadapters implemented? Are
they on forms or do you have a class that handles them or a .dll or what?
Are they all in the same place? Either way I think you could wrap the
connections in with them (unless they are on the form but it doesn't sound
like that's the case) and just share out the connection string That way
you'll have the events and everything with the class (although you'd still
have this if you instantiated it within your scope) and it's logically
grouped together.

Off of the top of your head though, do you plan on wiring anything in for
the connections event handlers? there are only two events that the
connection raises, InfoMessage and StateChange but you may need these
depending on the app.
 
Back
Top