2017-08-31 57 views
0

從多個數據庫的多個數據I有11訪問數據庫文件每一個具有20個項目需要被存儲在本地變量作爲 RejRsnStn(ⅰ)(原因)其中代表站數1至11 & 原因就代表理性1至20讀取在vb.Net

我在下面的方式讀取這些數據:

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 

能否請您指導我用簡潔的實現方法具d。

在此先感謝。 Prashant。

+1

將所有連接內容移到循環之外。 – Gustav

+0

令我難以置信的是,你使用循環作爲原因代碼,但完全無法將它們用於工作站ID。 –

+0

我投票結束這個問題作爲題外話,因爲不,你不能haz德codez。 –

回答

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 

然後,我會做一個子程序從數據庫中獲得二十代碼:

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. 

當然,我無法測試這一點,因爲我沒有訪問數據庫文件,但它應該工作,如果沒有,它可能需要很小的調整來實現你的目標。

希望這可以幫助你。