Tuesday, February 3, 2009

To get column names from a particular Table

Execute below query on a particular Database.

select column_name, data_type from information_schema.columns where table_name='myTableName'

which gives all the column names that Table.

Here the where condition is for getting details from the given Table.

To get Table names from a particular Database

Execute below query on a particular Database.

select table_name from Information_Schema.Tables where Table_Type='Base Table'

which gives all the table names.

Here the where condition is for excluding "Views" from the result set.

To get the database names from the Sql Server

Execute below query on "master" Database.

select [name] as [Database] from sysdatabases order by [name]

It gives all the Database names in the given server.

Connection string for c# code is :
"Data Source=localhost; Initial Catalog=master; uid=myID pwd=myPassword;"