Hello,
I am new to this group and I hope someone can help me *g*.
I am good in VBS but now I want to switch to vb.net because of getting crazy while creating every form in VBS with InternetExplorer an HTML.
OK. Now here is my first question:
I have an Access-Database and I want to get some information out of it.
The code works and looks loke this:
----------------------------------------------------------------------------------------------------------
Module Main
Dim i As Integer
Dim strUser As String = Mid(System.Security.Principal.WindowsIdentity.GetC urrent().Name(), System.Security.Principal.WindowsIdentity.GetCurre nt().Name().IndexOf("\") + 2).ToUpper
Dim objKategorien() As Kategorie
Const strDB As String = "D:\Dokumente und Einstellungen\Markus\Eigene Dateien\Visual Studio Projects\ResSolution-Zaehler\ResSolution-Zaehler.mdb"
Public Sub Main()
ReadDB()
End Sub
'Kategorien und akt. Zählerstand aus Access-Datenbank auslesen
Private Sub ReadDB()
Dim connDB1 As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLED B.4.0;Data Source=" & strDB)
Dim connDB2 As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLED B.4.0;Data Source=" & strDB)
Dim cmdDB1 As New OleDb.OleDbCommand("SELECT * from Kategorien", connDB1)
Dim cmdDB2 As New OleDb.OleDbCommand("SELECT Zaehlerstaende.Kategorie, Zaehlerstaende.Minuten, Zaehlerstaende.OK FROM(Zaehlerstaende) WHERE (Zaehlerstaende.[User-Kuerzel]='" & strUser & "')
and (Zaehlerstaende.Datum=Date())", connDB2)
Dim drDB1 As OleDb.OleDbDataReader
Dim drDB2 As OleDb.OleDbDataReader
connDB1.Open()
drDB1 = cmdDB1.ExecuteReader()
i = 0
Do While drDB1.Read()
ReDim Preserve objKategorien(i)
objKategorien(i) = New Kategorie
objKategorien(i).ID = drDB1.Item("ID")
objKategorien(i).Bezeichnung = drDB1.Item("Bezeichnung")
connDB2.Open()
drDB2 = cmdDB2.ExecuteReader()
Do While drDB2.Read
If (objKategorien(i).ID = drDB2.Item("Kategorie")) And (drDB2.Item("OK") = False) Then
objKategorien(i).Minuten = drDB2.Item("Minuten")
End If
Loop
drDB2.Close()
connDB2.Close()
i = i + 1
Loop
drDB1.Close()
connDB1.Close()
End Sub
End Module
----------------------------------------------------------------------------------------------
Public Class Kategorie
Public ID As Long
Public Bezeichnung As String
Public Minuten As Integer
End Class
----------------------------------------------------------------------------------------------
It is working very well but do I have to open and close the drDB2 every time?
And why is it not working only with connDB1? I have to Dim connDB2, too, because the compiler says: Dieser Verbindung ist bereits ein geöffneter DataReader zugeordnet, der zuerst geschlossen werden
muss. (The connection is already set to a DataReader witch has to be closed first).
Is there a better way to get the data. Is there something like a DataSet where I can store the result?
Please help me.
Thx, Markus Ellebrecht
P.S. I am german and my english might not be the best. So please foregive me my mistakes *g*