在运行时在VB.Net中设置数据库类

时间:2018-11-30 02:08:10

标签: vb.net

我有以下针对Sqlite和SqlServer的类:

SQLite 的类:

Imports System.Data.SQLite
Public Class clsOperDB_SQLite

 Public Shared Function getValue(sql As String) As String

        Try
            Using conn As New SQLiteConnection(strConn_SQLITE)
                Using cmd As New SQLiteCommand()
                    cmd.Connection = conn
                    conn.Open()
                    cmd.CommandText = sql
                    Return cmd.ExecuteScalar
                End Using
            End Using
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        Return ""

    End Function

End Class

SQLSERVER 的类:

Imports System.Data.SqlClient
Public Class clsOperDB_SQLSERVER

            Public Shared Function getValue(sql As String) As String

                Try
                    Using conn As New SqlConnection(strConn_SQLSERVER)
                        Using cmd As New SqlCommand()
                            cmd.Connection = conn
                            conn.Open()
                            cmd.CommandText = sql
                            Return cmd.ExecuteScalar
                        End Using
                    End Using
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try

                Return ""

            End Function

End Class

这是我的测试表格:

Public Class Form1

    'form level variable
    Dim dbConnector

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim connectionType As String = "SQLITE"

        ' need something like this or any way to set form level variable

        If connectionType = "SQLITE" Then
            dbConnector = clsOperDB_SQLite
        Else
            dbConnector = clsOperDB_SQLSERVER
        End If


    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        'get value from SQLITE
        Dim ValueFromDatabase As String = dbConnector.getValue("select .....")
    End Sub
End Class

我需要帮助来定义dbConnector变量并设置其值,intellisense还应向我展示类方法,使用我想更改数据库的参数,并避免在每次我要使用一个或另一个数据库的情况下都使用条件式: / p>

   Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        Dim Query As String = "Select ..."
        Dim ValueFromDatabase As String = ""

        ' dont want to use if for each sql query

        If connectionType = "SQLITE" Then
            ValueFromDatabase = clsOperDB_SQLite.getValue(Query)
        Else
            ValueFromDatabase = clsOperDB_SQLSERVER.getValue(Query)
        End If

    End Sub

两个类的其余方法和参数都是相同的,只是更改类数据对象(SQLiteConnection,SqlConnection等)

谢谢

1 个答案:

答案 0 :(得分:1)

您应该定义一个将所有普通成员都分类的接口。然后,您可以为实现该接口的每个数据源创建一个类。然后,在应用程序代码中,您可以声明该接口类型的变量,并将实现该接口类型的任何类的实例分配给该变量。然后,您可以使用该变量并调用接口的任何成员,而无需关心实际类实例的类型。

接口和实现类如下所示:

Public Interface IDataAccessProvider
    Function GetValue(sql As String) As String
End Interface

Public Class SqliteDataAccessProvider
    Implements IDataAccessProvider

    Public Function GetValue(sql As String) As String Implements IDataAccessProvider.GetValue
        'Provide SQLite-specific implementation here.
    End Function

End Class

Public Class SqlServerDataAccessProvider
    Implements IDataAccessProvider

    Public Function GetValue(sql As String) As String Implements IDataAccessProvider.GetValue
        'Provide SQL Server-specific implementation here.
    End Function

End Class

您的应用程序代码可能如下所示:

Private dataAccessProvider As IDataAccessProvider

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'Read a value that identifies the data source and store it here.
    'The value might be stored in the config file or wherever else is appropriate.
    Dim dataSourceIdentifier As String

    Select Case dataSourceIdentifier
        Case "SQLite"
            dataAccessProvider = New SqliteDataAccessProvider()
        Case "SQL Server"
            dataAccessProvider = New SqlServerDataAccessProvider()
    End Select
End Sub

然后,您可以只在代码中调用dataAccessProvider.GetValue,而不必担心数据源的实际含义,除非确保您的SQL语法对该数据源有效。

请注意,尽管您的工作完全取决于您,但我选择在此代码中使用合理的命名约定。没有人会使用clsOperDB_SQLSERVER这样的类名称来为我长时间工作。原因是您没有在.NET Framework中看到任何具有类似名称的类型。