C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » ADO.NET »

Get the table details


Posted Date: 22 Oct 2009    Resource Type: Code Snippets    Category: ADO.NET
Author: Shunmuganathan MMember Level: Diamond    
Rating: 1 out of 5Points: 10



We can get all the details of the table of the database.
Following code is use to achive it.

Namespace part

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


Following is the code part to get the table details for the SQLEXPRESS

Code Part

class SchemaTable
{
static void Main(string[] args)
{
string ConnectionString = "server=(local)\\SQLEXPRESS;database=Store;Integrated Security=SSPI";
SqlConnection MySqlConnection = new SqlConnection(ConnectionString);

MySqlConnection.Open();
SqlCommand MySqlCommand = new SqlCommand("select * from ItemMaster", MySqlConnection);
SqlDataReader MySqlDatareader = MySqlCommand.ExecuteReader();

DataTable MyDataTableSchema = MySqlDatareader.GetSchemaTable();

foreach (DataRow MyDataRowRow in MyDataTableSchema.Rows)
{
foreach (DataColumn MyDataColumn in schema.Columns){
Console.WriteLine(MyDataColumn.ColumnName + " = " + MyDataRowRow[col]);
}
}
MySqlDatareader.Close();
MySqlConnection.Close();
}
}



Code Explanation

1. Create the SQL Connection
2. Open Sql connection
3. Create SqlDataReader and SqlCommand,Data table
4. By using the Data reader getSchemaTable and assign it in the data table
5. By fection the data table we can get all the details of the table used in the query.
6. In the above example I show only column name but we can get all the details of the columnalso

Thanks
Nathan



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Table Schema  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Get all the table names from mdb file
Previous Resource: Data Relation in Sql using C#
Return to Discussion Resource Index
Post New Resource
Category: ADO.NET


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use