在运行时将数据库提供程序从MSSQL更改为SQLite [英] Change in runtime the database provider from MSSQL to SQLite
问题描述
我的VB.NET项目可以与多个数据库一起使用吗?
例如,我有2个项目,一个项目带有MSSQL数据库,另一个项目带有SQLite数据库.
我希望可以在两个项目中重复使用的共享类,
例如,带有MSSQL数据库的日志记录例程(简化):
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:
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
还有一个带有SQLite数据库的日志记录例程示例:
and a second logging routine example with a SQLite database:
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:
Dim cnDestination As New ADODB.Connection()
Dim rsDestination As New ADODB.Recordset()
和
and
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
推荐答案
理想情况下,您将拥有一个集中的请帮我给DB做到这一点"功能-该功能将照顾哪个DB在跟
说话
如果您有50个全部与DB离散地交谈的函数,那么您刚刚学到了有关函数重用和关注点分离的宝贵经验:)
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 :)
这篇关于在运行时将数据库提供程序从MSSQL更改为SQLite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!