[vb.net] GridViewControl mit Abfrage füllen

Gepostet am: Apr 12, 2011 12:44:58 PM

SQL2GridViewControl

Public Sub SQL2GridViewControl(ByVal MyGridView As DataGridView, ByVal SQLAbfrage As String, ByVal tblName As String)         Dim sDataSource As String = Application.StartupPath & "\<Name>.mdb"         Dim sConnString As String = _             "Provider=Microsoft.Jet.OLEDB.4.0;" & _             "Data Source=" & sDataSource & ";" & _             "User ID=Admin;" & _             "Password="         Dim cn As OleDb.OleDbConnection = New OleDb.OleDbConnection(sConnString)         Try 'versuche die Datenbank zu öffnen             cn.Open()         Catch ex As Exception             LogMessage(5, "Fehler beim Öffnen der Datenbank")             Exit Sub         End Try          Dim cmd As New OleDb.OleDbCommand         cmd.Connection = cn         cmd.CommandText = SQLAbfrage          'DataAdapter mit SelectCommand verbinden         Dim ODA As New OleDb.OleDbDataAdapter         ODA.SelectCommand = cmd          ' mit dem DataAdapter des DataSet füllen         Dim DS As New DataSet         ODA.Fill(DS, tblName)          'Daten an das DataGridView binden         MyGridView.DataSource = DS         MyGridView.DataMember = tblName  End Sub