|
|
Title | Use ADOX to search the table columns in an Access database for a string in Visual Basic .NET |
Description | This example shows how to use ADOX to search the table columns in an Access database for a string in Visual Basic .NET. |
Keywords | ADOX, Access, database, table, column, table columns, search columns, VB.NET |
Categories | Database |
|
|
First add references to the COM libraries:
- Microsoft ActiveX Data Objects 2.6 Library
- Microsoft ADO Ext. 2.6 for DLL and Security
(Or whatever your versions are.)
When you click the Search Tables button, the following code executes. It opens the database and uses the ADOX catalog to learn about the database. It loops through the Tables collection and looks for objects that have the TABLE type. (You could remove this test to make the code search queries and system tables in addition to normal tables.) The code searches TABLE type objects for the target string. If it finds the string, it lists the table's name and its columns in a ListView control.
|
|
Private Sub btnSearchTables_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _
btnSearchTables.Click
lvwTables.Items.Clear()
' Open the connection.
Dim conn As New ADODB.Connection
conn.ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Persist Security Info=False;" & _
"Data Source=" & txtDatabase.Text
conn.Open()
' Make a catalog for the database.
Dim cat As New ADOX.Catalog
cat.ActiveConnection = conn
' Search the catalog's views.
Dim target As String = txtSearchFor.Text.ToLower()
For i As Integer = 0 To cat.Tables.Count - 1
If cat.Tables(i).Type = "TABLE" Then
Dim cols As String = ""
For Each col As ADOX.Column In _
cat.Tables(i).Columns
cols &= ", " & col.Name
Next col
If cols.ToLower().IndexOf(target) >= 0 Then
Dim lvi As ListViewItem = _
lvwTables.Items.Add(cat.Tables(i).Name)
cols = cols.Substring(2)
lvi.SubItems.Add(cols)
End If
End If
Next i
conn.Close()
lvwTables.Columns(0).Width = -2
lvwTables.Columns(1).Width = -2
End Sub
|
|
|
|
|
|