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" %>
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;"
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: