ARTICLE

Getting a Database Tables

Posted by Mahesh Chand Articles | ADO.NET in VB.NET May 06, 2004
OleDb data provider's connection class (OleDbConnection) provides a method GetOleDbSchemaTable that returns a database schema. The code listed in Listing 1 shows you to call this method. I am using Access 2000 Northwind database.
 
Reader Level:

OleDb data provider's connection class (OleDbConnection) provides a method GetOleDbSchemaTable that returns a database schema. The code listed in Listing 1 shows you to call this method. I am using Access 2000 Northwind database.

To test this application, create a Windows application and add a DataGrid control to the form and write code listed in Listing 1 on the form load.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim str As String = "Provider=Microsoft.jet.oledb.4.0;Data Source=c:\\northwind.mdb"
Dim conn As New OleDb.OleDbConnection
conn.ConnectionString = str
' Open a connection
conn.Open()
'Call GetOleDbSchemaTable
Dim schemaTable As DataTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, _
New Object() {Nothing, Nothing, Nothing, "TABLE"})
' Attach data row to the grid and close the connection
DataGrid1.DataSource = schemaTable
conn.Close()
End Sub

Listing 1: Getting a Database Schema

The output of Listing 1 looks like Figure 1. As you can see from Figure 1 you get table names. types, description, creation date and so on.

Figure 1. Viewing a database schema.

Now if you want only database table names, you can extract the data table returned by the GetOleDbSchemaTable. The code listed in Listing 2 shows how to do so.

Dim str As String = "Provider=Microsoft.jet.oledb.4.0;Data Source=c:\\northwind.mdb"
Dim conn As New OleDb.OleDbConnectionconn.ConnectionString = str
' Open a connection
conn.Open()
Dim ca As Object
'Call GetOleDbSchemaTable
Dim schemaTable As DataTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, _New Object() {Nothing, Nothing, Nothing, "TABLE"})
Dim
tableList As New DataTable("Table")
Dim rowvals(0) As Object
Dim
newdc As New DataColumn("Col")
tableList.Columns.Add(newdc)
Dim rowcoll As DataRowCollection = tableList.Rows
Dim counter As Integer
For
counter = 0 To schemaTable.Rows.Count - 1
Dim rd As DataRow = schemaTable.Rows(counter)
If rd("TABLE_TYPE").ToString = "TABLE" Then
rowvals(0) = rd("TABLE_NAME").ToString
rowcoll.Add(rowvals)
End If
Next
' Attach data row to the grid and close the connection
DataGrid1.DataSource = tableList
conn.Close()

Listing 2. Extracting a DataTable to get only table names.

The output of Listing 2 looks like Figure 2.

share this article :
post comment
 

Post your question on forums.

Posted by Mahesh Chand Apr 07, 2009

i am very pleasure with ur answer . how to use calendar control for displaying the alarms to a particular date? I am doing my MCA final year . Please send me one new project for me.

Posted by Rajendra prasad Feb 16, 2008
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor