Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I want to load my checked listbox with column name of a table from Database.

Can anyone please help me with the sqlquery to retrieve only the column name in the table?

Thanks in Advance.
Posted
Updated 21-Jun-11 20:01pm
v2

try this

SQL
SELECT [name] AS [Column name]
FROM syscolumns
WHERE id = (SELECT id
FROM sysobjects
WHERE type = 'U'
AND [NAME] = 'Your Table Name ')
 
Share this answer
 
Comments
jayantbramhankar 22-Jun-11 3:02am    
Good and specific answer.
you can use following queyr to get the list of columns.
select * from information_schema.columns where table_name = 'customer'

its best to take this approach since Information schema views provide an internal, system table-independent view of the SQL Server metadata.

you can read more here
Information shema views[^]
 
Share this answer
 
v2
I suppose this will help you:

using System;
using System.Data;
using System.Data.SqlClient;

   class SchemaTable
   {
      static void Main(string[] args)
      {
         string connString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
         string sql = @"select * from employee";
         SqlConnection conn = new SqlConnection(connString);

         try {
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader reader = cmd.ExecuteReader();

            DataTable schema = reader.GetSchemaTable();

            foreach (DataRow row in schema.Rows)
            { 
               foreach (DataColumn col in schema.Columns){
                  Console.WriteLine(col.ColumnName + " = " + row[col]);
                  Console.WriteLine("Null value allowed: " + col.AllowDBNull);
               }
            }
            reader.Close();
         } catch(Exception e) {
            Console.WriteLine("Error Occurred: " + e);
         } finally {
            conn.Close();
         }
      }  
   }
 
Share this answer
 
v2
this[^] would have yielded 33,000,000 results in just 0.18 seconds, and here you have had waited too long.

Anurag
 
Share this answer
 
Refer this page for your answer :

Retriving Column name in SQL Query[^]
 
Share this answer
 
Use System view user_tab_columns to fetch the required info.

Select column_name, data_type from user_tab_columns where table_name = 'urtablename'
 
Share this answer
 
v2

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