Home All Groups Group Topic Archive Search About

Looping through all Tables in a Database

Author
13 Dec 2006 10:15 PM
Sherwood
Greetings,

I am using VB.NET 2003 and am simply trying to loop through all of the
tables in a SQL Server 2005 Database.  I am using the code below to
accomplish this.  However, all that is being returned in the "For Each" loop
is a "-" and a "1".   Any ideas as to what I am missing?

        sqlComm.CommandText = "Select name from sys.tables"
        strResult = sqlComm.ExecuteNonQuery()

        For Each x In strResult
            MessageBox.Show(x.ToString)
        Next

Thanks in advance!
--
Sherwood

Author
13 Dec 2006 10:49 PM
Spam Catcher
=?Utf-8?B?U2hlcndvb2Q=?= <Sherw***@discussions.microsoft.com> wrote in
news:82E0C1A1-ABF0-4077-BEE3-C1ACA93866C3@microsoft.com:

> I am using VB.NET 2003 and am simply trying to loop through all of the
> tables in a SQL Server 2005 Database.  I am using the code below to
> accomplish this.  However, all that is being returned in the "For
> Each" loop is a "-" and a "1".   Any ideas as to what I am missing?
>
>         sqlComm.CommandText = "Select name from sys.tables"
>         strResult = sqlComm.ExecuteNonQuery()
>
>         For Each x In strResult
>             MessageBox.Show(x.ToString)
>         Next

This won't work - I believe you'll need to look through a datareader or
dataset of the results.
Author
13 Dec 2006 11:18 PM
Sherwood
Thanks.  Using a DataReader seemed to solve the problem.  I used the
following code just in case you're interested.

sqlComm.CommandText = "Select Table_name from information_schema.tables"
Dim dr As SqlClient.SqlDataReader
dr = sqlComm.ExecuteReader

Do While dr.Read
    MsgBox(dr("Table_name"))
Loop

dr.Close()
--
Sherwood


Show quoteHide quote
"Spam Catcher" wrote:

> =?Utf-8?B?U2hlcndvb2Q=?= <Sherw***@discussions.microsoft.com> wrote in
> news:82E0C1A1-ABF0-4077-BEE3-C1ACA93866C3@microsoft.com:
>
> > I am using VB.NET 2003 and am simply trying to loop through all of the
> > tables in a SQL Server 2005 Database.  I am using the code below to
> > accomplish this.  However, all that is being returned in the "For
> > Each" loop is a "-" and a "1".   Any ideas as to what I am missing?
> >
> >         sqlComm.CommandText = "Select name from sys.tables"
> >         strResult = sqlComm.ExecuteNonQuery()
> >
> >         For Each x In strResult
> >             MessageBox.Show(x.ToString)
> >         Next
>
> This won't work - I believe you'll need to look through a datareader or
> dataset of the results.
>
>
Author
14 Dec 2006 12:49 AM
Spam Catcher
=?Utf-8?B?U2hlcndvb2Q=?= <Sherw***@discussions.microsoft.com> wrote in
Show quoteHide quote
news:B51B3E4B-7698-4BAA-9CFC-1452D487F83A@microsoft.com:

> Thanks.  Using a DataReader seemed to solve the problem.  I used the
> following code just in case you're interested.
>
> sqlComm.CommandText = "Select Table_name from
information_schema.tables"
> Dim dr As SqlClient.SqlDataReader
> dr = sqlComm.ExecuteReader
>
> Do While dr.Read
>     MsgBox(dr("Table_name"))
> Loop
>
> dr.Close()


This would be a bit better:


Dim dr As SqlClient.SqlDataReader = sqlComm.ExecuteReader

try
        Do While dr.Read
            MsgBox(dr("Table_name"))
        Loop
catch ex as exception
Finally
        dr.Close()
end try

Notice that the dr.close is between the Finally statement? This is good
practice to force a close of the datareader in ALL situations to prevent
orphaned connections. Orphaned datareader connections gobble resources
like no tomorrow ;)
Author
14 Dec 2006 4:38 AM
Cor Ligthert [MVP]
Spam Catcher,

> Notice that the dr.close is between the Finally statement? This is good
> practice to force a close of the datareader in ALL situations to prevent
> orphaned connections. Orphaned datareader connections gobble resources
> like no tomorrow ;)
>

But the not handled catch is in my opinin very bad code, I assume you know
it, but this can people reading this give them wrong idea's.

Cor

Show quoteHide quote
"Spam Catcher" <spamhoneypot@rogers.com> schreef in bericht
news:Xns9898C9B878348usenethoneypotrogers@127.0.0.1...
> =?Utf-8?B?U2hlcndvb2Q=?= <Sherw***@discussions.microsoft.com> wrote in
> news:B51B3E4B-7698-4BAA-9CFC-1452D487F83A@microsoft.com:
>
>> Thanks.  Using a DataReader seemed to solve the problem.  I used the
>> following code just in case you're interested.
>>
>> sqlComm.CommandText = "Select Table_name from
> information_schema.tables"
>> Dim dr As SqlClient.SqlDataReader
>> dr = sqlComm.ExecuteReader
>>
>> Do While dr.Read
>>     MsgBox(dr("Table_name"))
>> Loop
>>
>> dr.Close()
>
>
> This would be a bit better:
>
>
> Dim dr As SqlClient.SqlDataReader = sqlComm.ExecuteReader
>
> try
>    Do While dr.Read
>        MsgBox(dr("Table_name"))
>    Loop
> catch ex as exception
> Finally
>    dr.Close()
> end try
>
> Notice that the dr.close is between the Finally statement? This is good
> practice to force a close of the datareader in ALL situations to prevent
> orphaned connections. Orphaned datareader connections gobble resources
> like no tomorrow ;)
>
Author
14 Dec 2006 7:17 AM
Spam Catcher
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in
news:exVeelzHHHA.960@TK2MSFTNGP04.phx.gbl:

> But the not handled catch is in my opinin very bad code, I assume you
> know it, but this can people reading this give them wrong idea's.

Yup of course ;-)
Author
14 Dec 2006 2:06 PM
Paul Clement
On Wed, 13 Dec 2006 14:15:02 -0800, Sherwood <Sherw***@discussions.microsoft.com> wrote:

¤ Greetings,
¤
¤ I am using VB.NET 2003 and am simply trying to loop through all of the
¤ tables in a SQL Server 2005 Database.  I am using the code below to
¤ accomplish this.  However, all that is being returned in the "For Each" loop
¤ is a "-" and a "1".   Any ideas as to what I am missing?
¤
¤         sqlComm.CommandText = "Select name from sys.tables"
¤         strResult = sqlComm.ExecuteNonQuery()
¤
¤         For Each x In strResult
¤             MessageBox.Show(x.ToString)
¤         Next
¤
¤ Thanks in advance!

An alternative would be to use GetOleDbSchemaTable. I don't typically recommend running queries
directly against system tables.

        Dim DatabaseConnection As New System.Data.OleDb.OleDbConnection
        Dim SchemaTable As DataTable

        DatabaseConnection.ConnectionString = "Provider=sqloledb;" & _
                                                "Data Source=(local);" & _
                                                "Initial Catalog=Northwind;" & _
                                                "Integrated Security=SSPI"

        DatabaseConnection.Open()

        SchemaTable =
DatabaseConnection.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, _
                                                            New Object() {Nothing, Nothing, Nothing,
Nothing})

        Dim RowCount As Int32

        For RowCount = 0 To SchemaTable.Rows.Count - 1
            Console.WriteLine(SchemaTable.Rows(RowCount)!TABLE_NAME.ToString)
        Next RowCount

        DatabaseConnection.Close()


Paul
~~~~
Microsoft MVP (Visual Basic)