2

Alright, I know that this case has been discussed all over the web, but I still don't understand.

Here's my search result so far:

"SELECT table_name AS Name FROM INFORMATION_SCHEMA.Tables WHERE TABLE_TYPE = 'BASE TABLE'";

In this SQL code, I thought that the INFORMATION_SCHEMA was a database name and when I executed the code (of course with the connected database name), an exception was thrown saying that the database name doesn't exist.

Updated:

When I run the SQL query and execute it with:

OleDbDataReader reader = cmd.ExecuteReader();

It says: Could not find: path/INFORMATION_SCHEMA.mdb;

The things I already done and they're good are:

  1. The connection string is filled with the database name.
  2. I can do the usual SQL CommandText to my database.
  3. I'm using the OLEDB DBMS.

SOLVED

And my final code with help of other people is:

DataTable MySchemaTable = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
new Object[] { null, null, null, "TABLE" });

for (int i = 0; i < MySchemaTable.Rows.Count; i++) {
combobox1.Items.Add(MySchemaTable.Rows[i].ItemArray[2].ToString()); }

Another search is this:

`DataTable T = con.GetSchema("Tables");`

And when I print out the t.ToString();, it's only showwing the Tables.

And many more searches.

CASE CLOSED!

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Geofff
  • 27
  • 2
  • 10

4 Answers4

0

INFORMATION_SCHEMA is not a database name, it is a schema that exists in every database. It contains views that give you information about the database structure.

An information schema view is one of several methods SQL Server provides for obtaining metadata. Information schema views provide an internal, system table-independent view of the SQL Server metadata. Information schema views enable applications to work correctly although significant changes have been made to the underlying system tables. The information schema views included in SQL Server comply with the ISO standard definition for the INFORMATION_SCHEMA.

Your query will give you the names of all tables and in your connection string you should specify the database name for which you want to get all tables.

More about INFORMATION_SCHEMA on MSDN.

Szymon
  • 42,577
  • 16
  • 96
  • 114
0

Do you can to try to use the database before you start your query ? Like this :

USE [databaseName]
GO
SELECT table_name AS Name FROM INFORMATION_SCHEMA.Tables WHERE TABLE_TYPE = 'BASE TABLE'

So you can be sure , you are connected to the good database before start the query

Mehdi Bugnard
  • 3,889
  • 4
  • 45
  • 86
0

You can not use the INFORMATION_SCHEMA in OLEDB, it has a different way to provide its table information. Take a look at GetOleDbSchemaTable. It looks like:

MySchemaTable = MyConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, _
                   New Object() {Nothing, Nothing, Nothing, "TABLE"})

The Documentation states here:

The .NET Framework Data Provider for OLE DB also exposes schema information by using the GetOleDbSchemaTable method of the OleDbConnection object. As arguments, GetOleDbSchemaTable takes an OleDbSchemaGuid that identifies the schema information to return, and an array of restrictions on those returned columns. GetOleDbSchemaTable returns a DataTable populated with the requested schema information.

CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
0

No Need for all those troubles. You can just follow this answer

To check if a table exists you can extend DbConnection like this:

public static class DbConnectionExtensions
{
    public static bool TableExists(this DbConnection conn, string table)
    {
        conn.open();
        var exists = conn.GetSchema("Tables", new string[4] { null, null, table, "TABLE" }).Rows.Count > 0;
        conn.close();
        return exists;
    }
}

Then you can call TableExists in any derived class like OleDbConnection, SQLiteConnection or SqlConnection.

Community
  • 1
  • 1
Ghasem
  • 14,455
  • 21
  • 138
  • 171