Re: Connection.GetSchema

Tech-Archive recommends: Fix windows errors by optimizing your registry



To check for a table in a sql server database use the sp_tables stored
procedure.
The following example iterates through all tables and uses a datareader
object.

If you need to first get a list of all databases on the server, use the
sp_databases stored procedure first and iterate through that, for each
database that exists, check for the existence of your tables with the code
below.

----------

Try
Dim sqlConn As New
SqlConnection("Server=servername;Database=db_name;Trusted_Connection=True")

Dim cmd As New SqlCommand("sp_tables", sqlConn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@table_name", "%")
cmd.Parameters.AddWithValue("@table_owner", "dbo")

Dim dr As SqlDataReader
sqlConn.Open()
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
While dr.Read
'== do something with the table name here
dr.Item("TABLE_NAME").ToString
End While

dr.Close()

sqlConn.Dispose()

Catch ex As Exception

If sqlConn.State = ConnectionState.Open Then sqlConn.Close()
sqlConn.Dispose()
MessageBox.Show(ex.Message)

End Try


"Jiho Han" <jihohan@xxxxxxxxx> wrote in message
news:ed1a6e0222b8c82df17b264f92@xxxxxxxxxxxxxxxxxxxxx
I am trying to test for the existence of a particular table in all
databases on a server.

I found some samples and looking at the SDK doc as well, I found the
following use of GetSchema method on Connection class:

SqlConnection.GetSchema(string collectionName, string[] restrictions)

So, I would do something like,

DataTable dt = connection.GetSchema("Tables", new string[] {null, null,
"MyTable"});

which returns nothing.

Curiously enough, if I try the OleDbConnection instead of SqlConnection,
it returns results that I expect. I am guessing that there is a
difference in how GetSchema is implemented for SqlConnection and
OleDbConnection.

Any ideas?
Thanks

Jiho




.



Relevant Pages

  • Create SharePoint Portal failed.
    ... One mentioned ensuring that SQL Server uses a case ... 13:55:40 Service database server is 'USDC-JOHRIV'. ... Update dbo.propertylist set DisplayName = N'Last name' ...
    (microsoft.public.sharepoint.portalserver)
  • Re: ADO Connection Timeout
    ... to the central server, but you are willing to live with periods where it ... i.e. a local database or even a text file. ... to function until the connection can be restored to the server. ...
    (microsoft.public.data.ado)
  • Web Developers - Happy Hearts And HDTV! - Lockergnome
    ... Certificate on your MSIIS Web server. ... getting data from a database is only half the problem. ... Zend recently started a series about building rock solid code in PHP. ... which provides bulk database conversion. ...
    (freebsd-questions)
  • Re: TNS could not resolve the connect identifier
    ... This database resides on Machine A. ... The Web server is running on Machine B. ... Using tnsping is not as good as using a real connection such as via ... client (note that this is terminology that appears in the 10g R2 ...
    (comp.databases.oracle.server)
  • Config for OLTP system
    ... extrenal disks fo the 60GByte database server. ... IBM Informix Dynamic Server Configuration Parameters ... # BUFFSIZE - OnLine no longer supports this configuration parameter. ...
    (comp.databases.informix)