Friday, February 25, 2011

Database Table schema

onsidering you want to display field names from DB , just get top(0) rows which will give u just the schema.
 below is the code for same which retrives only column names with no time.
  
 
string sqlQuery = "Select top(0) from tableName"// top(0) gives u just the schema ie. column name from table
   
  using(SqlConnection conn = new SqlConnection())
 {
 
  conn.Open();
  Using(SqlCommand cmd = new SqlCommand(sqlQuery,Conn))
  {
  SqlDatareader reader = cmd.ExecuteReader();
  DataTable dt = new DataTable();
  dt.load(reader);
   //fill the listbox with column name
  
   ForEach(DataColumn column In dt.Columns)
  {
    listbox1.Items.Add(column.ColumnName);
  }
}
}
 
 
Yes you right, right now you want all columns so just give wind card character * . change the below query as shown below

"Select top(0)* from tableName"

No comments :

Post a Comment