Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Please Anyone tell me how to retrieve a single column name from sql table using c#. please Reply as quick as possible. please provide the code too.
Posted
Updated 23-Jun-12 11:09am
v2

You can retrieve all the columns for a table with a simple SQL statement:
SQL
SELECT column_name FROM information_schema.columns WHERE table_name = 'My Table Name'
 
Share this answer
 
Comments
P.Vinoth 23-Jun-12 3:05am    
Thanks for your answer but is show the output as columnname 0 and datatype and all. i just want to retrieve the column name alone
OriginalGriff 23-Jun-12 3:16am    
That is because you didn't bother to follow instructions. Just replace the My Table Name part with teh name of your tables, and insert the sql into your normal C# code.
It will return a single column table with each column name in a separate row.
P.Vinoth 23-Jun-12 3:12am    
Please give the coding in c# its not working
P.Vinoth 23-Jun-12 3:46am    
ya its shows the otuput when i specified the column name. i will explain clearly. i just know the tablename. i dont know what are the column it has so i just want to know the column name and retrieve it
OriginalGriff 23-Jun-12 4:34am    
Yes. And if you run the query it gives you the name of every column in the table you specify.
Which is exactly what you are asking for...
Or are you after something else and expressing yourself badly?
Remember that we can't see your screen, access your HDD, or read your mind.
You can Try like this
C#
SqlConnection con=con = new SqlConnection(//your connection string here
);
SqlCommand cmd = new SqlCommand(Select * from [My Table Name], con);
                      SqlDataReader  sdr = cmd.ExecuteReader();

                  string  columnName = sdr.GetName(0);

Here you will get your first column name in to the "columnName" variable, by changing the index you will get different column names which are present in your table.
I hope it will helps you.
 
Share this answer
 
Comments
sanjay001 30-Jul-13 7:45am    
how to fetch the column name to the dynamically created control (label) in asp.net
pls put solution
XML
Fetch the column name from table and assign it to the label(VB.NET).

Dim c As New SqlConnection("<connection string>")
c.open()
Dim command As SqlCommand = c.CreateCommand()
command.CommandText ="SELECT name FROM sys.columns WHERE object_id = OBJECT_ID('<column name>') and column_id='<column id>' "
Dim reader As SqlDataReader = command.ExecuteReader()
reader.Read()
Label1.text = reader.Item(0).ToString
c.close()
 
Share this answer
 
v2
Comments
TrushnaK 15-Jan-14 7:38am    
you are too late... question asked on 23-Jun-12 1:52am.
Karthik_Mahalingam 15-Jan-14 8:17am    
Yes shrik36,
whenever you are posting a solution, pls check the posted date...
Shrikant S 21-Jan-14 8:57am    
Thank you to both of you dear for reminding but I wasn't satisfied with the replies above. Although its late reply to the one who actually asked but may help others.
Member 8288246 19-Feb-16 13:23pm    
Totally helped me. Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900