Retrieve Column Names From Table

Posted by Nkkppp under ASP.NET on 9/16/2012 | Points: 10 | Views : 1643 | Status : [Member] | Replies : 7
Is there a way to obtain the column names of a particular table using entity? What I mean is that I can obtain it via SQL with
CODE
SELECT Column_Name FROM Information_Schema.Columns WHERE TABLE_Name = 'TableName'


I attempted to obtain the information via Reflection but it returns the column names, as well as the association names.

What I am doing is populating a combo box with the field names.

Provide C# code.Thank you.

REgards,
Prathap.




Responses

Posted by: Hariinakoti on: 9/17/2012 [Member] Starter | Points: 25

Up
0
Down
follow this link.
http://stackoverflow.com/questions/452464/how-can-i-get-column-names-from-a-table

Thanks & Regards
Hari

Nkkppp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Oswaldlily on: 9/17/2012 [Member] Starter | Points: 25

Up
0
Down
SELECT COLUMN_NAME
FROM information_schema.columns
WHERE table_name = 'tablename'
ORDER BY ordinal_position

Nkkppp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vikash on: 9/17/2012 [Member] Starter | Points: 25

Up
0
Down
Please use following simply.........

Sp_help Bill_master

or

select column_name 'Column Name', data_type 'Data Type', character_maximum_length 'Maximum Length' from information_schema.columns where table_name = 'table_name'

please mark as answer if it is helpful.......

Regards,
Vikash Pathak

Nkkppp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nkkppp on: 9/17/2012 [Member] Starter | Points: 25

Up
0
Down
I think I am not clear with the question.

To retrieve column names from sql we can just use below query

select name from syscolumns where id=object_id('TableName ')

But I am using Entityframework, so I am askin how to retrieve the column names that are properties.

Nkkppp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pgayath on: 9/18/2012 [Member] Starter | Points: 25

Up
0
Down
hi Prathap,

This might help you

IEnumerable<FieldList> properties = from p in typeof(T).GetProperties()
where (from a in p.GetCustomAttributes(false)
where a is EdmScalarPropertyAttribute
select true).FirstOrDefault()
select new FieldList
{
FieldName = p.Name,
FieldType = p.PropertyType,
FieldPK = p.GetCustomAttributes(false).Where(a => a is EdmScalarPropertyAttribute && ((EdmScalarPropertyAttribute)a).EntityKeyProperty).Count() > 0
};


A good answer and explanation to your query in the below link..
http://stackoverflow.com/questions/6056597/entity-framework-how-do-i-get-the-columns

Hope this solves ur problem.

Thanks and Regards,
Gayathri P

Nkkppp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 9/29/2012 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi

refer this url


http://www.dotnetfunda.com/codes/code3293-how-to-bind-particular-table-columns-bind-the-gridview-and-dropdownlist.aspx


Mark as Answer if its helpful to you

Kumaraspcode2009@gmail.com

Nkkppp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 9/29/2012 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi
try this
Please mark as answer if it helpful to you.


Mark as Answer if its helpful to you

Kumaraspcode2009@gmail.com

Nkkppp, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response