从vb.Net中的多个数据库中读取多个数据

时间:2017-08-31 08:55:47

标签: vb.net ms-access vb.net-2010

我有11个Access数据库文件,每个文件有20个项目需要存储在本地变量中 RejRsnStn(i)(原因)其中 i 代表站号1到11& 原因代表原因1到20。

我正在通过以下方式阅读这些数据:

Public Sub ReadReasonCodes()
    'Station 1
    For Reason = 1 To 20
        DatafileStn = "E:\DATANetwork\DATAStation_1.accdb;Jet OLEDB:Database Password=xxxxxxxx"
        Dim ConnstringStn As String = provider & DatafileStn
        connstringwrkstn = ConnstringStn
        myConnection.ConnectionString = ConnstringStn
        myConnection.Open()

        str = "SELECT * FROM Table_Config WHERE StationNo = 1"
        cmd = New OleDbCommand(str, myConnection)
        dr = cmd.ExecuteReader

        While dr.Read()
            RejRsnStn1(Reason) = If(IsDBNull(dr("RejectionReason" & Reason)), "NA", dr("RejectionReason" & Reason))
        End While
        myConnection.Close()
    Next

    'Station 2
    For Reason = 1 To 20
        DatafileStn = "E:\DATANetwork\DATAStation_2.accdb;Jet OLEDB:Database Password=xxxxxxxx"
        Dim ConnstringStn As String = provider & DatafileStn
        connstringwrkstn = ConnstringStn
        myConnection.ConnectionString = ConnstringStn
        myConnection.Open()

        str = "SELECT * FROM Table_Config WHERE StationNo = 2"
        cmd = New OleDbCommand(str, myConnection)
        dr = cmd.ExecuteReader

        While dr.Read()
            RejRsnStn2(Reason) = If(IsDBNull(dr("RejectionReason" & Reason)), "NA", dr("RejectionReason" & Reason))
        End While
        myConnection.Close()
    Next

End Sub

请你用简洁的方法指导我。

提前致谢。 PRASHANT。

1 个答案:

答案 0 :(得分:1)

我会做这样的事情:

首先,我将导入System.Data.OleDb,然后构造一个包含所有代码的类:

Imports System.Data.OleDb
Public Class rCode
    Dim _ReasonCode As String = ""
    Public Property ReasonCode() As String
        Get
            Return _ReasonCode
        End Get
        Set(value As String)
        _ReasonCode = value
        End Set
    End Property
End Class

Public Class RCodes
    Public Property Station_1_Reasoncodes As New List(Of rCode)
    Public Property Station_2_Reasoncodes As New List(Of rCode)
    Public Property Station_3_Reasoncodes As New List(Of rCode)
    Public Property Station_4_Reasoncodes As New List(Of rCode)
    Public Property Station_5_Reasoncodes As New List(Of rCode)
    Public Property Station_6_Reasoncodes As New List(Of rCode)
    Public Property Station_7_Reasoncodes As New List(Of rCode)
    Public Property Station_8_Reasoncodes As New List(Of rCode)
    Public Property Station_9_Reasoncodes As New List(Of rCode)
    Public Property Station_10_Reasoncodes As New List(Of rCode)
    Public Property Station_11_Reasoncodes As New List(Of rCode)
End Class

然后我会发起我刚刚创建的课程:

public class form1
    Public ReasonCodes As RCodes = New RCodes

然后我会创建一个子程序来从数据库中获取20个代码:

public class form1
    Public ReasonCodes As RCodes = New RCodes

    Public Sub GetReasonCodes(station As String)
    Dim Provider As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
    Dim myConnection As New OleDbConnection
    Dim DatafileStn As String = "E:\DATANetwork\DATAStation_" & station & ".accdb;Jet OLEDB:Database Password=xxxxxxxx"
    Dim ConnstringStn As String = Provider & DatafileStn
    myConnection.ConnectionString = ConnstringStn
    myConnection.Open()
    Dim cmd As New OleDbCommand("SELECT * FROM Table_Config WHERE StationNo = " & station, myConnection)
    Dim dr As OleDbDataReader = cmd.ExecuteReader
    Dim s As rCode = New rCode
    Dim s2 As New List(Of rCode)
    While dr.Read()
        For Reason = 1 To 20
            If IsDBNull(dr("RejectionReason" & CStr(Reason))) Then s.ReasonCode = "NA" Else s.ReasonCode = CStr(dr("RejectionReason" & CStr(Reason)))
            s2.Add(s)
        Next
    End While
    If Not dr.IsClosed Then dr.Close()
    myConnection.Close()
    dr = Nothing
    myConnection = Nothing
    Select Case station
        Case "1"
            ReasonCodes.Station_1_Reasoncodes = s2
        Case "2"
            ReasonCodes.Station_2_Reasoncodes = s2
        Case "3"
            ReasonCodes.Station_3_Reasoncodes = s2
        Case "4"
            ReasonCodes.Station_4_Reasoncodes = s2
        Case "5"
            ReasonCodes.Station_5_Reasoncodes = s2
        Case "6"
            ReasonCodes.Station_6_Reasoncodes = s2
        Case "7"
            ReasonCodes.Station_7_Reasoncodes = s2
        Case "8"
            ReasonCodes.Station_8_Reasoncodes = s2
        Case "9"
            ReasonCodes.Station_9_Reasoncodes = s2
        Case "10"
            ReasonCodes.Station_10_Reasoncodes = s2
        Case "11"
            ReasonCodes.Station_11_Reasoncodes = s2
    End Select
End Sub

然后,我会成为发起人:

    Public Sub ReadReasonCodes()
        For i As Integer = 1 To 11
            GetReasonCodes(CStr(i))
        Next
    End Sub
End Class

您可以从“ReasonCodes”类中访问您的数据

EXA:

Dim Stn6_code15 = ReasonCodes.Station_6_Reasoncodes(14).ReasonCode
'Note: since reasoncodes are stored in an Array, they are 0 based. 
'So ReasonCodes.Station_6_Reasoncodes(14).ReasonCode = 
'station 6, reason code 15.

当然,我无法测试这个,因为我没有访问数据库文件,但它应该可以工作,如果没有,可能需要进行小的调整才能获得目标。

希望这会对你有所帮助。