ASP 101 - Active Server Pages 101 - Web03
The Place ASP Developers Go!



Windows Technology Windows Technology
15 Seconds
4GuysFromRolla.com
ASP 101
ASP Wire
VB Forums
VB Wire
WinDrivers.com
internet.commerce internet.commerce
Partners & Affiliates
ASP 101 is an
internet.com site
ASP 101 is an internet.com site
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash



 Top ASP 101 Stories Top ASP 101 Stories
What is ASP?
VBScript Classes: Part 1 of N
Migrating to ASP.NET

QUICK TIP:
Connection Pooling lightens the load
Show All Tips >>
ASP 101 RSS Feed ASP 101 Updates


Quick Tips


How To List The Tables In A Database

While developers are usually interested in accessing the data contained within a database's tables, occasionally you need to know something about the tables themselves. It's at these times when this quick tip might come in handy.

The magic behind this tip is the GetSchema method of the connection object. It'll give you all sorts of information about the database to which you are currently connected. I'm not going to go into it in depth because that would turn this quick tip into a long article, but I will give you a sample code snippet which will list all the tables in the current database.

<%@ Page Language="VB" %>
<script runat="server">

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim cnnTables As New System.Data.SqlClient.SqlConnection
        cnnTables.ConnectionString = "Data Source=localhost;Initial Catalog=DB_NAME;User ID=USER;Password=PASS;"

        cnnTables.Open()

        gridTables.DataSource = cnnTables.GetSchema("Tables")
        gridTables.DataBind()

        cnnTables.Close()
    End Sub

</script>
<html>
<head>
    <title>List Tables In Database</title>
</head>
<body>
<form id="myForm" runat="server">

<asp:GridView ID="gridTables" runat="server" />

</form>
</body>
</html>

In order to get information about your database you'll obviously need to use your own connection string, but aside from that the code is pretty straight-forward. If you want to learn more about the GetSchema method and filtering your results via restrictions, you're probably best off heading straight to the source and checking out the documentation from Microsoft:


Previous      Show All Tips      Next

If you have a tip you would like to submit, please send it to: webmaster@asp101.com.


Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers