I have a DB (in SQL Server 2008 SP3) and need all Schema names, Table names and Column Names in related hierarchy in C# Code, I have the SQLElement Class as Following:
public class SQLElement
{
public string SchemaName { get; set; }
public string TableName { get; set; }
public string ColumnName { get; set; }
}
And have a List Like:
List<SQLElement> SQLElementCollection = new List<SQLElement>();
So how can I read Names from DB and add them to this List (SQLElementCollection)?
for example assume we create a table like this:
Create Table [General].[City] (
[Id] BIGINT NOT NULL IDENTITY(1, 1),
[Title] NVARCHAR(30) NOT NULL DEFAULT (N''),
[Province_Id] BIGINT NOT NULL
)
and I need the list like:
[0]={SchemaName="General", TableName="City", ColumnName="Id"}
[1]={SchemaName="General", TableName="City", ColumnName="Title"}
[2]={SchemaName="General", TableName="City", ColumnName="Province_Id"}
Does any one have any idea about this?
Edit:
In next step how we can get the type of each column or related properties?
My suggestion is to include another member
DataTypeinSQLElementif you have change permission or create another class with a property nameDataTypeand then inherit fromSQLElementand then save data type name into it for later use and use below query for all information, thanks