|
|
Title | Use ADOX to search the queries in an Access database for a string in Visual Basic .NET |
Description | This example shows how to use ADOX to search the queries in an Access database for a string in Visual Basic .NET. |
Keywords | ADOX, Access, database, query, command text, search query, 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 Queries button, the following code executes. It opens the database and uses the ADOX catalog to learn about the database. It loops through the Views collection and searches each query's command text for the target string. If it finds the string, it lists the query's name and command text in a ListView control.
|
|
Private Sub btnSearchQueries_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _
btnSearchQueries.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.Views.Count - 1
Dim query As String = _
cat.Views(i).Command.CommandText
query = query.Replace(vbCr, " ").Replace(vbLf, " ")
If query.ToLower().IndexOf(target) >= 0 Then
Dim lvi As ListViewItem = _
lvwTables.Items.Add(cat.Views(i).Name)
lvi.SubItems.Add(query)
End If
Next i
conn.Close()
lvwTables.Columns(0).Width = -2
lvwTables.Columns(1).Width = -2
End Sub
|
|
|
|
|
|