-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Get all Column Names
PostPosted: Wed Feb 21, 2007 12:48 pm 
Newbie

Joined: Fri Dec 01, 2006 11:36 am
Posts: 6
I need to get all Column Names or Property Names of one table. Is there a
way to get them with a statement or sql query?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 21, 2007 9:58 pm 
Regular
Regular

Joined: Tue Aug 08, 2006 4:28 am
Posts: 96
Location: Hong Kong
If you are using NHibernate and all classes are well mapped, you would try the following

Code:
System.Collections.IDictionary cm = sessionFactory.GetAllClassMetadata();
foreach (Type type in cm.Keys)
{
    NHibernate.Persister.Entity.AbstractEntityPersister persister =
        (NHibernate.Persister.Entity.AbstractEntityPersister)cm[type];

    string[] properties = persister.PropertyNames;
}



If you are using SQL query, you would try this for SQL Server 2000
Code:
SELECT syscolumns.*
FROM sysobjects INNER JOIN syscolumns ON sysobjects.id = syscolumns.id
WHERE sysobjects.name = 'YourTableName'
ORDER BY syscolumns.colid


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 22, 2007 5:47 am 
Newbie

Joined: Fri Dec 01, 2006 11:36 am
Posts: 6
I found 2 other ways:

The code above helps to get all column names, these ones get all column names of one Class/Table

Code:
public void getHeaders(Type type)
{

            NHibernate.Metadata.IClassMetadata cm = SessionProvider.Instance._sessionFac.GetClassMetadata(type);
           
            foreach (string s in cm.PropertyNames)
            {
                    Console.WriteLine(s);
            }
}


Another way with reflection:


Code:
public void getHeadersByReflection(Type type)
{

          foreach (PropertyInfo i in type.GetProperties())
         {
              Console.WriteLine(i.Name);
          }

}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.