2

What are database vendor independent ways to obtain list of tables in C# code?

I heard about three:

  1. ADO .NET, IDbConnection.GetSchema("Tables")
  2. Linq, new System.Data.Linq.DataContext(IDbConnection).Mapping.GetTables(); (>=3.5, System.Data.Linq.dll).
  3. EF, Entity Framework - Get List of Tables

But this mean that somewhere there are a set of adapters for syntax of each vendor.

Is there a standard SQL syntax for this operation?

What are other ways to get list of tables in C#?

Community
  • 1
  • 1
user1709408
  • 528
  • 4
  • 16

1 Answers1

3

You could use ANSI standard Information Schema Views:

SELECT *
FROM INFORMATION_SCHEMA.TABLES;

Supported by: SQL Server, Postgresql, MySQL, ...

Not implemented by Oracle, DB2, SQLite,...

Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275
  • which standart defines this syntax? I think it is not vendor independent (it's microsoft-related) – user1709408 Jan 09 '16 at 12:55
  • No it isn't not only SQL Server. It is ANSI standard implemented by Postgreql, MySQL, SQL Server, ... – Lukasz Szozda Jan 09 '16 at 12:55
  • @user1709408 . . . It is standard and will work on SQL Server, Postgres, MySQL, and probably some other databases. It don't think it works on Oracle, MS Access, or DB2. – Gordon Linoff Jan 09 '16 at 12:56
  • Thanks! Is there a link to the text of standart? i think, that it's name is "SQL:2003 core feature F021 Basic information schema (ANSI/ISO SQL:2003 standard Part 11 Schemata)" – user1709408 Jan 09 '16 at 13:03
  • http://www.iso.org/iso/home/store/catalogue_ics/catalogue_detail_ics.htm?csnumber=53685 – user1709408 Jan 09 '16 at 13:12
  • @user1709408 As I thought original is not free. And your second link is draft version `THIS DOCUMENT IS STILL UNDER STUDY AND SUBJECT TO CHANGE. IT SHOULD NOT BE USED FOR REFERENCE PURPOSES.` – Lukasz Szozda Jan 09 '16 at 13:24
  • @user1709408: it's a **standard** (with a **d** at the end) - not a "standart" .... – marc_s Jan 09 '16 at 15:35
  • http://english.stackexchange.com/questions/25423/why-do-non-native-english-users-often-spell-standard-as-standart – user1709408 Jan 09 '16 at 15:47
  • @marc Probably comment above from `user1709408` is for you. – Lukasz Szozda Jan 09 '16 at 16:01
  • SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'; for Postgresql. – ihsan güç Dec 20 '22 at 09:04