Has anyone tried this?

My general dilemma is having to open and maintain multiple connections across multiple forms.

I have an MDI application and I would prefer to store the connection in the parent form and just have the child forms use that connection if it needs it. This normally wouldn't be a problem except that the application can have many different data source types (a SQL Server or a Access Database). So simply using a delegate won't work because I can't cast the connection to the correct connection type until run time.

I have a feeling what I will have to do is use a delegate with the type of Object , and just use a if (Connection is _______) type of thing and cast it inside of the if statement and then call whatever functions the form needs to use the connection object.

Any other ideas or solutions that someone has used that they care to share?

6 Years
Discussion Span
Last Post by zachattack05

Using IDBConnection, IDbTransaction, IDbCommand, IDbDataParameter and IDataParmeter you can access most of the functionality of the various data providers without knowing which one you are actually using. So at runtime, instead of saving your connection as a SqlConnection (or OleDbConnection, etc.) you'd use and IDbConnection variable.

As for sharing it, create a static DB class object with methods you need to use and call it from your various forms. This way you decouple your forms from each other.

Edited by Momerath: n/a



Seems like interfaces keep poking their nasty little head up and staring at me.

Since I've learned about them, my code production/output has been terrible...I keep thinking an interface would be useful in situations where I'm not sure if it is, or just complicates things.

I'll read up on those. Thanks!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.