Click here to Skip to main content
15,882,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ok I have a application I am developing at the moment. Its purpose is to be a helper application so it will automatically create each of the following:
StoredProcedures for Database
Class Files for
windows forms for

It works perfectly with MySql databases and I can get it to work also with MsSql but my question is this.... in my code I need to declare two separate connections for mysql and mssql as follows:

Protected DbConnectionMySql As MySqlConnection
Protected DbConnectionMsSql As SqlClient.SqlConnection

I then proceed to use these as follows:

If (frmMain.SQLServerType = "mysql") Then
                Me.dtSchema = Me.DbConnectionMySql.GetSchema("Columns")
            ElseIf frmMain.SQLServerType = "mssql" Then
                Me.dtSchema = Me.DbConnectionMsSql.GetSchema("Columns")
            End If

This is easy as its only a few lines but its painful with longer code.
I would like to map or Cast each connection to a single variable that can be used so I only have to code once.

I am thinking a generic type that implements the IDBConnection interface would work but its kinda confusing me.... any idea, directions, help is appreciated!

This was meant to be a helper application to automate the more time consuming tasks of developing larger applications.

You should use the .NET Framework Data Providers (ADO.NET)[^], with allows you to connect to all kinds of databases.

Share this answer
IDbConnection is the correct way. Instead of having to variables DbConnectionMySql and DbConnectionMsSql, you'll use only one:
Dim _Connection as IDbConnection.
When the connection object is created, you still have to distinguish:
If (frmMain.SQLServerType = "mysql") Then
    _Connection = new MySqlConnection()
    _Connection = new SqlClient.SqlConnection()

By the way, I'd create an enum for the database types.
Share this answer
You should be using the factory pattern[^] with the ADO.Net Factory classes. Here[^] is an aexcellent article to start with.

Good luck!
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900