ARTICLE

Get a database table properties

Posted by Mahesh Chand Articles | ADO.NET in C# January 22, 2001
Get a table properties such as column names, types etc using DataColumn and DataTable.
Reader Level:
Download Files:
 

This program displays you a table properties such as its column names, types, and column properties. I have used a database "mcTest.mdb" which has a table called 'Developer'. You can download this attached database and change the path of the database according to your location.

Explaination

I have used ADODataSetCommand, DataSet, DataTable, and DataColumn classes to do so. See my forthcoming tutorial on ADO.NET for more details of these classes.

private void Form1_Load(object sender, System.EventArgs e)
{
string strDSN = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\mcTest.MDB";
string strSQL = "SELECT * FROM Developer" ;
// create Objects of ADOConnection and ADOCommand
OleDbConnection myConn = new OleDbConnection(strDSN);
OleDbDataAdapter myCmd = new OleDbDataAdapter( strSQL, myConn );
myConn.Open();
DataSet dtSet = new DataSet();
myCmd.Fill( dtSet, "Developer" );
DataTable dt = dtSet.Tables[0];
listBox1.Items.Add("Field Name DataType Unique AutoIncrement AllowNull");
listBox1.Items.Add("=================================");
foreach( DataColumn dc in dt.Columns )
{
listBox1.Items.Add(dc.ColumnName+" , "+dc.DataType +" ,"+dc.Unique +" ,"+dc.AutoIncrement+" ,"+dc.AllowDBNull );
}
}

How to Run?

  • Download source code and database zip files and unzip them.
  • Change the database path  in this string.  "Provider=Microsoft.JET.OLEDB.4.0;data source=C:\\Mahesh\\mcb.mdb");
  • Run the application

Login to add your contents and source code to this article
post comment
     
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.