Obtain the names and fields of all tables in Access data.

Source: Internet
Author: User

--------------
// Obtain the Access database table name
Public void GetTableName ()
{
String connString = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" + @ "C: \ Information Technology Test Result. mdb ";
OleDbConnection conn = new OleDbConnection (connString );

Conn. Open ();
DataTable dt = conn. GetOleDbSchemaTable (OleDbSchemaGuid. Tables, new object [] {null, "TABLE "});

Conn. Close ();
Int n = dt. Rows. Count;
String [] tableName = new string [n];
Int m = dt. Columns. IndexOf ("TABLE_NAME ");
For (int I = 0; I <n; I ++)
{
DataRow dr = dt. Rows [I];
TableName [I] = dr. ItemArray. GetValue (m). ToString ();

This. cbxtable. Items. Add (tableName [I]. ToString ());
}
This. cbxtable. SelectedIndex = 0;


}

 

/// <Summary>
/// Return all field names of a table
/// </Summary>
Public List <string> GetTableColumn (string tableName)
{
String connString = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" + @ "C: \ information technology test. mdb ";
OleDbConnection conn = new OleDbConnection (connString );
List <string> list = new List <string> ();
DataTable dt = new DataTable ();
Try
{

Conn. Open ();
Dt = conn. GetOleDbSchemaTable (OleDbSchemaGuid. Columns, new object [] {null, null, tableName, null });
Int n = dt. Rows. Count;
String [] strTable = new string [n];
Int m = dt. Columns. IndexOf ("COLUMN_NAME ");
For (int I = 0; I <n; I ++)
{
DataRow dr = dt. Rows [I];
List. Add (dr. ItemArray. GetValue (m). ToString ());
}
Return list;
}
Catch (Exception ex)
{
Throw ex;
}
Finally
{
Conn. Close ();
}
}
Private void button3_Click (object sender, EventArgs e)
{

String str = this. cbxtable. SelectedItem. ToString ();
{
List <string> lst = GetTableColumn (str );
ListBox1.DataSource = lst;

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.