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]);
}
}
}
}
|