.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ADO.Net >> Post New QuestionBookmark and Share Subscribe to Forum

How to get table column name from dataset table

Posted By: Pratima B     Posted Date: March 23, 2010    Points:2   Category :ADO.Net
hi,

I need to know the column name from one of my dataset table? Can i do it from C#?

Responses
Author: Lalij Mer             
Posted Date: March 25, 2010     Points: 5   

Hi,
you can try this code sample.



using System;
using System.Collections;
using System.Data;
using System.IO;
using System.Xml.Serialization;
using System.Windows.Forms;
using System.Data.SqlClient;


public class MainClass
{
static abstract SqlConnection con;

static void Main()
{
con = new SqlConnection("Server=(local)\\SQLEXPRESS;Initial Catalog=MyDatabase;Integrated Security=SSPI");

string str = "SELECT ID, FirstName, LastName FROM Employee";
SqlCommand cmd = new SqlCommand(str, con);

SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();

da.Fill(ds, "Employee");

// Display the column names
DataColumn dc;
foreach ( dc in ds.Tables(0).Columns) {
Console.Write("{0,15}", dc.ColumnName);
}

// Add a newline after the column headings
Console.Write(vbCrLf);

// Display the data for each row. Loop through the rows first.
DataRow dr;
foreach ( dr in ds.Tables(0).Rows) {

// Then loop through the columns for the current row.
int i;
for (i = 1; i <= ds.Tables(0).Columns.Count; i++) {
Console.Write("{0,15}", dr(i - 1));
}

// Add a line break after every row
Console.Write(vbCrLf);
}
}
}


More reference you can look here

http://msdn.microsoft.com/en-us/library/system.data.datacolumn.columnname.aspx
http://forums.asp.net/p/1170547/1959521.aspx#1959521

Please Check my answer if you help....
Thank You...

Lalit.


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend