This message was discovered on microsoft.public.dotnet.framework.adonet.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
| Richard Ruggiere |
| GOOD ANSWER |
I want to get column names. I only see a reference to a column collection in the DataTable object model. Can I get the column name from any other object model or do I have to create a DataTable whenever I want to get column information?
|
|
|
| |
|
|
| |
| |
| Donny Mack (VIP) |
| GOOD ANSWER |
Hey Richard,
I thought of a couple ways you can do this using ADO.NET objects. 2 ways using the DataReader and 1 using the DataSet. Below is the code:
<script language="C#" runat="server">
protected void Page_Load( object sender, EventArgs e ) {
this.GetColumNames_DataReader(); this.GetColumnNames_DataReader_SchemaOnly(); this.GetColumnNames_DataSet();
}
protected void GetColumnNames_DataReader_SchemaOnly() {
System.Data.SqlClient.SqlConnection SqlCon = new System.Data.SqlClient.SqlConnection( "server=localhost;database=northwind;trusted_connection=true" ); System.Data.SqlClient.SqlCommand SqlCmd = new System.Data.SqlClient.SqlCommand( "SELECT * FROM Products", SqlCon );
SqlCon.Open();
System.Data.SqlClient.SqlDataReader SqlReader = SqlCmd.ExecuteReader(System.Data.CommandBehavior.SchemaOnly); System.Int32 _columncount = SqlReader.FieldCount;
System.Web.HttpContext.Current.Response.Write( "<b><u>SqlDataReader Columns Using System.Data.CommandBehavior.SchemaOnly</u></b>" ); System.Web.HttpContext.Current.Response.Write( "<br>" );
for ( System.Int32 iCol = 0; iCol < _columncount; iCol ++ ) {
System.Web.HttpContext.Current.Response.Write( "Column " + iCol.ToString() + ": " ); System.Web.HttpContext.Current.Response.Write( SqlReader.GetName( iCol ).ToString() ); System.Web.HttpContext.Current.Response.Write( "<br>" );
}
}
protected void GetColumNames_DataReader() {
System.Data.SqlClient.SqlConnection SqlCon = new System.Data.SqlClient.SqlConnection( "server=localhost;database=northwind;trusted_connection=true" ); System.Data.SqlClient.SqlCommand SqlCmd = new System.Data.SqlClient.SqlCommand( "SELECT * FROM Products", SqlCon );
SqlCon.Open();
System.Data.SqlClient.SqlDataReader SqlReader = SqlCmd.ExecuteReader(); System.Int32 _columncount = SqlReader.FieldCount;
System.Web.HttpContext.Current.Response.Write( "<b><u>SqlDataReader Columns</u></b>" ); System.Web.HttpContext.Current.Response.Write( "<br>" );
for ( System.Int32 iCol = 0; iCol < _columncount; iCol ++ ) {
System.Web.HttpContext.Current.Response.Write( "Column " + iCol.ToString() + ": " ); System.Web.HttpContext.Current.Response.Write( SqlReader.GetName( iCol ).ToString() ); System.Web.HttpContext.Current.Response.Write( "<br>" );
}
}
protected void GetColumnNames_DataSet() {
System.Data.SqlClient.SqlConnection SqlCon = new System.Data.SqlClient.SqlConnection( "server=localhost;database=northwind;trusted_connection=true" ); System.Data.SqlClient.SqlDataAdapter SqlCmd = new System.Data.SqlClient.SqlDataAdapter( "SELECT * FROM Products", SqlCon ); System.Data.DataSet ds = new System.Data.DataSet(); SqlCmd.Fill( ds );
System.Web.HttpContext.Current.Response.Write( "<b><u>DataSet Columns</u></b>" ); System.Web.HttpContext.Current.Response.Write( "<br>" );
for ( System.Int32 iCol = 0; iCol < ds.Tables[0].Columns.Count; iCol ++ ) {
System.Web.HttpContext.Current.Response.Write( "Column " + iCol.ToString() + ": " ); System.Web.HttpContext.Current.Response.Write( ds.Tables[0].Columns[iCol].ColumnName ); System.Web.HttpContext.Current.Response.Write( "<br>" );
}
}
</script>
Hope this helps,
-- Donny Mack - MCP, MCSD, Microsoft .NET MVP DotNetJunkies.com 504.606.6225 ________________________________________________ Site - dotnetjunkies.com/ Training - dotnetjunkies.com/training Newsletter - dotnetjunkies.com/join.aspx Books and Online Chapters - dotnetjunkies.com/dotnetbooks
Learn ASP.NET - Read the books... Programming Data-Driven Web Applications with ASP.NET http://www.amazon.com/exec/obidos/ASIN/0672321068/dotnetjunkies-20/
ASP.NET: Tips, Tutorials and Code http://www.amazon.com/exec/obidos/ASIN/0672321432/dotnetjunkies-20/
Coming November 2001 Professional ADO.NET http://www.amazon.com/exec/obidos/ASIN/186100527X/dotnetjunkies-20/
"Richard Ruggiere" <Click here to reveal e-mail address> wrote in message news:#ia0$CvZBHA.2408@tkmsftngp05... > I want to get column names. I only see a reference to a column collection in > the DataTable object model. Can I get the column name from any other object [Original message clipped]
|
|
|
| |
|
|
| |
| |
| Richard Ruggiere |
| GOOD ANSWER |
This helpped alot. I am still learning the object models and never figured the table object was w/in the dataset object. Thanks for the help.
*** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it!
|
|
|
| |
|
|
| |
|
|
| |
| hamis |
| GOOD ANSWER |
use master database(SYSTEM) stored procedure(s); exec sp_columns 'tablename'
[Original message clipped]
|
|
|
| |
|
|
| |
|
|
|
|
|