Code:
Public Class frmMain
Dim stConnectionString As String = ""
Dim oDatabase As System.Data.OleDb.OleDbConnection ''' <summary>
''' Structure Item provides relevant Information about a Component such as Name, Class and
''' Description.
''' </summary>
''' <remarks>According to those Information Fields you have to ensure those columns exist in
''' the component Database! (Required Columns are ENTRY, Description, Class)</remarks>
Protected Structure Item
Private stName As String
Private stDescription As String
Private stClassName As String
Public Sub New(ByVal Name As String, ByVal Description As String, ByVal ClassName As String)
Me.stName = Name
Me.stDescription = Description
Me.stClassName = ClassName
End Sub 'New
Public Overrides Function ToString() As String
Return Me.stName
End Function 'ToString
Public Function GetDescription() As String
Return "Klasse: " & Me.stClassName & " Beschreibung: " & Me.stDescription
End Function 'GetDescription
End Structure 'Item
''' <summary>
''' When loading the Form first of all the connection String to the component database is read. After
''' that the datbase is opened and all symbol data is read to the application.
''' </summary>
''' <param name="sender">The Control or the Source which fired the event. Typical frmMain.</param>
''' <param name="e">Additional Event Information. Empty here</param>
''' <remarks>none yet.</remarks>
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtStatus.Text = "Reading Component database..."
stConnectionString = Me.FetchConnectionString
txtStatus.Text = "Connecting to Database"
Me.ConnectToDatabase()
txtStatus.Text = "Reading Symbols"
Me.ReadSymbols()
txtStatus.Text = "Finished reading Symbols from Database"
End Sub 'frmMain_Load
''' <summary>
''' Close the database connection when the application finishes.
''' </summary>
''' <param name="sender">Control or source which fired the event.</param>
''' <param name="e">Additional Event Information.</param>
''' <remarks>none yet</remarks>
Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
txtStatus.Text = "Close database Connection"
Me.DisconnectDatabase()
End Sub 'frmMain_FormClosing
''' <summary>
''' When the selected Index in lsbSymbols changes (User selects a new symbol), the Entry in the table "GATE" of the
''' component database is evaluated. With this Entry the according components in the table "ComponentData" can
''' be evaluated. With each entry in ComponentData a Item Structure is created and added to lsbComponents.
''' </summary>
''' <param name="sender">Control or source which fired the event.</param>
''' <param name="e">Additional Event Information.</param>
''' <remarks>none yet</remarks>
Private Sub lsbSymbols_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lsbSymbols.SelectedIndexChanged
Dim cmdItems As System.Data.OleDb.OleDbCommand
Dim rdPointer As System.Data.OleDb.OleDbDataReader
Dim rdComponents As System.Data.OleDb.OleDbDataReader
If oDatabase.State = ConnectionState.Open Then
cmdItems = New System.Data.OleDb.OleDbCommand("SELECT ENTRY FROM Gate WHERE CCTNAME = '" & lsbSymbols.SelectedItem.ToString & "'", oDatabase)
rdPointer = cmdItems.ExecuteReader()
cmdItems.Dispose()
lsbComponents.Items.Clear()
While rdPointer.Read
cmdItems = New System.Data.OleDb.OleDbCommand("SELECT ENTRY, Description, Class FROM ComponentData WHERE LPNTR = '" & rdPointer.GetString(0) & "'", oDatabase)
rdComponents = cmdItems.ExecuteReader
While rdComponents.Read
Me.lsbComponents.Items.Add(New Item(rdComponents.GetString(0), rdComponents.GetString(1), rdComponents.GetString(2)))
End While
End While
If lsbComponents.Items.Count > 0 Then
lsbComponents.SelectedIndex = 0
End If
End If
End Sub 'lsbSymbols_SelectedIndexChanged
''' <summary>
''' Show additional component information in status bar when a device is selected.
''' </summary>
''' <param name="sender">Control or source which fired the event.</param>
''' <param name="e">Additional Event Information.</param>
''' <remarks></remarks>
Private Sub lsbComponents_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lsbComponents.SelectedIndexChanged
txtStatus.Text = CType(lsbComponents.SelectedItem, Item).GetDescription
End Sub 'lsbComponents_SelectedIndexChanged
''' <summary>
''' Stellt eine Verbindung zu E3.series her und liest den ConnectionString zur aktuell verwendeten Datenbank.
''' </summary>
''' <returns>Eine System.String Darstellung des ConnectionStrings zur aktuellen Bauteildatenbank.</returns>
''' <remarks></remarks>
Private Function FetchConnectionString() As String
Dim e3Application As New e3.e3Application
FetchConnectionString = e3Application.GetComponentDatabase
While System.Runtime.InteropServices.Marshal.ReleaseComObject(e3Application) <> 0
End While
e3Application = Nothing
GC.Collect()
GC.WaitForPendingFinalizers()
End Function 'FetchConnectionString
''' <summary>
''' Establishes a connection to the component database.
''' </summary>
''' <remarks></remarks>
Private Sub ConnectToDatabase()
Try
oDatabase = New System.Data.OleDb.OleDbConnection(stConnectionString)
oDatabase.Open()
Catch
MessageBox.Show("Error Connecting to Database")
End Try
End Sub 'ConnectToDatabase
''' <summary>
''' Closes the connection to the component database
''' </summary>
''' <remarks></remarks>
Private Sub DisconnectDatabase()
oDatabase.Close()
oDatabase.Dispose()
End Sub 'DisconnectDatabase
''' <summary>
''' Reads all symbol names from the component database and applies them to
''' lsbSymbols.
''' </summary>
''' <remarks></remarks>
Private Sub ReadSymbols()
Dim cmdSymbols As System.Data.OleDb.OleDbCommand
Dim rdSymbols As System.Data.OleDb.OleDbDataReader
If oDatabase.State = ConnectionState.Open Then
cmdSymbols = New System.Data.OleDb.OleDbCommand("SELECT CCTNAME FROM Gate", oDatabase)
rdSymbols = cmdSymbols.ExecuteReader()
While rdSymbols.Read
If Not Me.lsbSymbols.Items.Contains(rdSymbols.GetString(0)) Then
lsbSymbols.Items.Add(rdSymbols.GetString(0))
End If
End While
End If
lsbSymbols.Enabled = True
End Sub 'ReadSymbols
End Class 'frmMain