Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Would it be possible to use my VB.NET project with multiple databases?

I have for example 2 projects, one project with a MSSQL database and another with a SQLite database.
I would like shared classes that can be re-used in both projects,

For example a logging routine (simplified) with a MSSQL database:
VB
Public Shared Sub fLOG(ByVal LUsername As String, ByVal LLogDesc As String, ByVal LProfileNumber As Integer)

    Dim cnDestination As New ADODB.Connection()
    Dim rsDestination As New ADODB.Recordset()
    Dim sSQL As String

    Try

        cnDestination.ConnectionString = fGetConnectionString()
    cnDestination.Open()

    sSQL = "INSERT INTO LOG" _
    & " (LogDT, LogUserID, LogDesc, LogProfileID)" _
    & " VALUES" _
      & " (" _
        & "'" & Format(System.DateTime.Now, "yyyy-MM-dd HH:m:ss:fff") & "'," _
        & "'" & LUsername & "'," _
        & "'" & LLogDesc & "'," _
        & "'" & LProfileNumber & "'" _
      & " )"
    cnDestination.Execute(sSQL)

    cnDestination.Close()

    Catch ex As Exception
    MsgBox("Error while logging")
    Finally
        cnDestination = Nothing
    End Try
End Sub


and a second logging routine example with a SQLite database:
VB
Public Sub fLOG(ByVal LUsername As String, ByVal LLogDesc As String, ByVal LProfileNumber As Integer)

    Dim cnDestination As New SQLite.SQLiteConnection
    Dim daDestination As New SQLite.SQLiteDataAdapter
    Dim sqlCommand As New SQLite.SQLiteCommand
    Dim sSQL As String

    Try
        cnDestination.ConnectionString = ConnectionStrings.fGetConnectionString()
        cnDestination.Open()

        sSQL = "INSERT INTO LOG" _
                    & " (LogDT, LogUserID, LogDesc, LogProfileID)" _
                & " VALUES" _
                    & " (" _
                        & "'" & Format(System.DateTime.Now, "yyyy-MM-dd HH:m:ss:fff") & "'," _
                        & "'" & LUsername & "'," _
                        & "'" & LLogDesc & "'," _
                        & "'" & LProfileNumber & "'" _
                    & " )"
        sqlCommand.Connection = cnDestination
        sqlCommand.CommandText = sSQL

        cnDestination.Close()

    Catch ex As Exception
        MsgBox("Error while logging")
    Finally
        cnDestination = Nothing
    End Try

End Sub



And the problem I'm having is that the database declaration is done with:
VB
Dim cnDestination As New ADODB.Connection()
Dim rsDestination As New ADODB.Recordset()

and
VB
Dim cnDestination As New SQLite.SQLiteConnection
Dim daDestination As New SQLite.SQLiteDataAdapter
Dim sqlCommand As New SQLite.SQLiteComm


So I need to choose in advance what database I'm going to use.

Who can point me in the direction, in have re-usable code for both projects so I can choose the database during runtime.

Kind regards
Posted
Comments
[no name] 5-Aug-12 17:06pm    
So write a class for one database and another class for another database. What is the problem?
Homebunny 5-Aug-12 17:20pm    
Well I have more then 50 functions over 20 shared classes, and I was hoping there is a more code friendly way of doing this...

1 solution

Ideally, you'd have a centralised 'do this to the DB for me please' function - that function would take care of which DB it was talking to

If you've got 50 functions that all talk discretely to the DB then you've just learned a valuable lesson about function reuse and concern-separation :)
 
Share this answer
 
Comments
Homebunny 6-Aug-12 2:22am    
Well barney, i understand perfect what you're saying, lesson is learned. But now the next question is: how can we deal with this to make things better, with a real live example.

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