|
Imports System.Data.OleDb
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnExecute.Click
ExecuteSQL()
End Sub
Private Sub ExecuteSQL()
'Connectionstring festlegen mit Hilfe des
'ConnectionStringBuilders
Dim builder As OleDbConnectionStringBuilder = New _
OleDbConnectionStringBuilder
With builder
.DataSource = "Nordwind.mdb"
.Provider = "Microsoft.Jet.OLEDB.4.0"
End With
'ConnectionString zusammenstellen
Dim CONNSTRING As String = builder.ConnectionString
'Verbindung aufbauen
Dim conn As New OleDbConnection(CONNSTRING)
conn.Open()
'Den Select-Command entgegennehmen und die Tabelle füllen
Dim da As New OleDbDataAdapter(txtSQLString.Text, conn)
Dim tbl As New DataTable
'*** Using conn ***
'**********************
'sorgt dafür, dass die Verbindung wieder geschlossen wird,
'wenn der Using-Block wieder verlassen wird.
Using conn
Try
da.Fill(tbl)
Catch ex As OleDbException
MessageBox.Show(ex.Message, "Fehler bei der "_
"Befehlsausführung:", MessageBoxButtons.OK, _
MessageBoxIcon.Exclamation)
End Try
'Datenbindung ohne BindungSource:
'Die DataTable wird direkt an die DataSource gebunden
DataGridView1.DataSource = tbl
lblCount.Text = tbl.Rows.Count.ToString
End Using
'*** End Using ***
'*******************
'Hier angekommen.
'wird die Connection automatisch wieder geschlossen
End Sub
'DataRow auslesen
'Das Ereignis wird ausgelöst bei Doppelklick in eine Zelle
Private Sub DataGridView1_CellContentClick(ByVal sender As _
System.Object, ByVal e As _
System.Windows.Forms.DataGridViewCellEventArgs) _
Handles DataGridView1.CellContentClick
'Die DataTable
Dim tbl As DataTable = DirectCast(DataGridView1.DataSource, _
DataTable)
'Die DataRow der DataTable per Rows(e.RowIndex) ermitteln
Dim obj As Object = DataGridView1.Rows(e.RowIndex)
'Die Ansicht der DataRow (DataRowView)
Dim rowView As DataRowView = _
TryCast(DataGridView1.Rows(e.RowIndex).DataBoundItem, _
DataRowView)
If rowView Is Nothing Then
Return
End If
'Die eigentliche DataRow
Dim row As DataRow = rowView.Row
'StringBuilder instanzieren
Dim sb As New System.Text.StringBuilder
'Durch die einzelnen Spalten der Tabelle iterieren
'und mittels StringBuilder festhalten
For Each spalte As DataColumn In row.Table.Columns
'Spaltennamen
sb.Append(spalte.ColumnName)
'Spaltentyp
sb.Append(" (" & spalte.DataType.ToString & "): ")
'Inhalt des Datenfeldes
sb.Append(row(spalte.ColumnName).ToString)
'Zeilenumbruch
sb.Append(vbNewLine)
Next
'Datenzeile ausgeben
MessageBox.Show(sb.ToString, "Datenzeile:", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
'Ein paar Beispiele für SQL-SELECT-Statements
With cboSelect
.Items.Add("SELECT * FROM Bestellungen")
.Items.Add("SELECT * FROM Kunden")
.Items.Add("SELECT Firma, BestellNr, "_
"Bestelldatum FROM Kunden, Bestellungen WHERE "_
"Kontaktperson = 'Thomas Hardy'")
.Items.Add("SELECT BestellNr, Bestelldatum FROM Kunden, "_
"Bestellungen WHERE Firma = 'Frankenversand'")
.Items.Add("SELECT Firma, BestellNr, Bestelldatum FROM "_
"Kunden, Bestellungen WHERE Land = 'Deutschland'")
.Items.Add("SELECT Firma, Telefon, Strasse, Plz, Ort FROM "_
"Kunden WHERE Land = 'Deutschland' AND PLZ LIKE '5%'")
.Items.Add("SELECT * FROM Kunden WHERE Ort = 'Madrid'")
.Items.Add("SELECT * FROM Kunden WHERE Ort LIKE 'M%'")
.Items.Add("SELECT Land, Plz, Ort, Strasse FROM Kunden "_
WHERE Strasse LIKE '%place%'")
.Items.Add("SELECT * FROM Kunden WHERE Ort LIKE 'M%' AND "_
"Kontaktperson LIKE 'Hanna%'")
.Items.Add("SELECT * FROM Kunden ORDER BY Ort")
.Items.Add("SELECT * FROM Kunden WHERE KundenID = 'GODOS'")
.Items.Add("SELECT KundenID, Ort, Telefon, TeleFax FROM "_
"Kunden WHERE Kontaktperson = 'Sven Ottlieb'")
.Items.Add("SELECT Firma, Kontaktperson, FirmaPosition "_
"FROM Kunden WHERE Land = 'Deutschland'")
.Items.Add("SELECT Firma, BestellNr, Bestelldatum FROM "_
"Kunden, Bestellungen WHERE Land = 'Deutschland' AND "_
"Lieferdatum > #05/01/1996#")
.Items.Add("SELECT Land, Firma, BestellNr, Bestelldatum "_
"FROM Kunden, Bestellungen WHERE Land = 'Deutschland' AND "_
"Lieferdatum > #006/01/1996#")
.Items.Add("SELECT BestellNr, Bestelldatum, Firma FROM "_
"Kunden, Bestellungen WHERE Firma = 'Frankenversand' AND "_
"Bestelldatum > #01/01/1996#")
.Items.Add("SELECT BestellNr, KundenID, Bestelldatum FROM "_
"Bestellungen WHERE Bestelldatum > #08/11/1995#")
.Items.Add("SELECT BestellNr, KundenID, Bestelldatum FROM "_
"Bestellungen WHERE Bestelldatum < #08/11/1995#")
.Items.Add("SELECT BestellNr, KundenID, Bestelldatum FROM "_
"Bestellungen WHERE Bestelldatum BETWEEN #08/11/1995# "_
"AND #01/01/1996#")
.Items.Add("SELECT BestellNr, Bestelldatum FROM Kunden, "_
"Bestellungen WHERE Kontaktperson = 'Henriette Pfalzheim'")
.Items.Add("SELECT BestellNr, KundenID, Bestelldatum FROM "_
"Bestellungen WHERE Bestelldatum BETWEEN #08/11/1995# AND "_
"#01/01/1996#")
.Items.Add("SELECT BestellNr, KundenID, Bestelldatum FROM "_
"Bestellungen WHERE Bestelldatum BETWEEN #08/11/1995# AND "_
"#01/01/1996#")
End With
cboSelect.SelectedIndex = 0
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _
cboSelect.SelectedIndexChanged
txtSQLString.Text = cboSelect.SelectedItem.ToString
ExecuteSQL()
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class | |