Rellenar un TreeView con el schema de una DB Access
Artículo por Club Developers · 11 mayo 2006
2131 vistas
Veamos cómo rellenar un TreeView con la estructura de una base de datos Access.
vbnet
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ' extraemos la estructura Dim lstStructure As New SortedList Dim MiConn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\User\NWIND.MDB;") MiConn.Open() Dim schemaTables As DataTable = MiConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, Nothing, "TABLE"}) Dim MiEnumCol, MinEnumTable As IEnumerator Dim schemaColTable As DataTable Dim ListCol As ArrayList MiEnumTable = schemaTables.Rows.GetEnumerator While MiEnumTable.MoveNext schemaColTable = MiConn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, New Object() {Nothing, Nothing, CType(MiEnumTable.Current, DataRow).Item("Table_Name"), Nothing}) ListCol = New ArrayList MiEnumCol = schemaColTable.Rows.GetEnumerator While MiEnumCol.MoveNext ListCol.Add(CType(MiEnumCol.Current, DataRow).Item("column_name")) End While lstStructure.Add(CType(MiEnumTable.Current, DataRow).Item("Table_Name"), ListCol) End While ' rellenado del TreeView Dim NodRoot, NodTable, NodTemp As TreeNode NodRoot = Me.TreeView1.Nodes.Add("biblio.mdb") NodRoot.ImageIndex = 0 Dim cmpt1, cmpt2 As Int32 Dim MiEnum As IEnumerator For cmpt1 = 0 To lstStructure.Count - 1 NodTable = NodRoot.Nodes.Add(CType(lstStructure.GetKey(cmpt1), String)) MiEnum = CType(lstStructure.GetByIndex(cmpt1), ArrayList).GetEnumerator While MiEnum.MoveNext NodTemp = New TreeNode With NodTemp .Text = CType(MiEnum.Current, String) .ImageIndex = 2 End With NodTable.Nodes.Add(NodTemp) NodTable.ImageIndex = 1 End While Next Me.TreeView1.ImageList = Me.ImageList1 End Sub