Click here to Skip to main content
15,891,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ok I have a vb.net 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 VB.net
windows forms for vb.net

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:

VB
Protected DbConnectionMySql As MySqlConnection
Protected DbConnectionMsSql As SqlClient.SqlConnection

I then proceed to use these as follows:

VB
If (frmMain.SQLServerType = "mysql") Then
                Me.DbConnectionMySql.Open()
                Me.dtSchema = Me.DbConnectionMySql.GetSchema("Columns")
                Me.DbConnectionMySql.Close()
            ElseIf frmMain.SQLServerType = "mssql" Then
                Me.DbConnectionMsSql.Open()
                Me.dtSchema = Me.DbConnectionMsSql.GetSchema("Columns")
                Me.DbConnectionMsSql.Close()
            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.
Posted

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

Piet
 
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:
VB
If (frmMain.SQLServerType = "mysql") Then
    _Connection = new MySqlConnection()
Else
    _Connection = new SqlClient.SqlConnection()
EndIf

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