German Wear Discount Shop - Click Here Write for Dotnet-friends and earn for your each submission [Dot]Net-Friends
Skip Navigation Links
Home
Latest
Fast Code
Articles
Tutorials
Online Resources
Forums
Login   | Hi, Guest


How to Get all Table names of a Database using ADO. NET?
Written By Dotnet Friends On 07/11/2007

Getting all Table names of database using System.Data.OleDb and System.Data.SqlClient Objects

Views: 1897
Rating: 4.2
Login to Rate
dotnet-friends
Tagged Under: ADO .NET, Database, SQL Server 2005, T-SQL

Explanation:

 

Getting all table Names from Access database:

using System;
using System.Data;
using System.Data.OleDb;

public class DatabaseInfo {    
 public static void Main () { 
   String connect = "Provider=Microsoft.JET.OLEDB.4.0;data source=.\\Employee.mdb";
   OleDbConnection con = new OleDbConnection(connect);
   con.Open();  
   Console.WriteLine("Made the connection to the database");

   Console.WriteLine("Information for each table contains:");
   DataTable tables = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,new object[]{null,null,null,"TABLE"});

   Console.WriteLine("The tables are:");
   foreach(DataRow row in tables.Rows
     Console.Write("  {0}", row[2]);


   con.Close();
 }
}

Getting all Table Names from SQL Server:

 

using System;
using System.Data;
using System.Data.OleDb;

public class DatabaseInfo {    
public static void Main () { 

string myConnStr = ConfigurationManager.ConnectionStrings["YOUR_ConnectionStringName"].ConnectionString;

string qurey = @"SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA ='dbo'";

try{

using (SqlConnection connection = new SqlConnection(myConnStr))

{

using (SqlCommand command = new SqlCommand(qurey, connection))

{

command.CommandType = CommandType.Text;

connection.Open();

SqlDataReader reader = command.ExecuteReader();

DataTable myTable = new DataTable("myTable");

myTable.Load(reader);

Console.WriteLine("The tables are:");
   foreach(DataRow row in myTables.Rows
     Console.Write("  {0}", row[2]);

}

}

     }
}


Delicious Digg reddit reddit Technorati
About the Author:

@@ Omer Kamal is a Software Developer at Eidanza GmbH Germany. He is Founder of FriendsPoint.de and Dotnet-Friends.com. He is currently Involved with SharePoint 2007, SharePoint Services 3.0 and BI Portal Solutions (Microsoft Dynamics Customization).
Check Dotnet Friends Profile

Related Useful Links:
Visitors/Readers Comments
(for questions please use The Forum)



mahi
its a good one . its very useful ...... Thanks

Hope more from you...

07/06/2008 04:20:31 UTC




Add your Comments

Name:  
Message:
Note: For faster response please use Forums >> for your questions instead of the comments area! (Admin)