|
|
Title | Use ADOX to list the queries in an Access database and give their command text in Visual Basic .NET |
Description | This example shows how to use ADOX to list the queries in an Access database and give their command text in Visual Basic .NET. |
Keywords | ADOX, Access, database, query, command text, 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 List 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 (this contains the queries) adding the query names and command text to a ListView control.
|
|
Private Sub btnListQueries_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _
btnListQueries.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
' List the catalog's queries.
For i As Integer = 0 To cat.Views.Count - 1
Dim lvi As ListViewItem = _
lvwTables.Items.Add(cat.Views(i).Name)
Dim txt As String = cat.Views(i).Command.CommandText
txt = txt.Replace(vbCr, " ").Replace(vbLf, " ")
lvi.SubItems.Add(txt)
Next i
conn.Close()
lvwTables.Columns(0).Width = -2
lvwTables.Columns(1).Width = -2
End Sub
|
|
|
|
|
|