zoukankan      html  css  js  c++  java
  • 获取Access数据里所有表的名称和表的字段

    --------------
    //获取Access数据库表名
            public void GetTableName()
            {
                    string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + @"C:\信息技术考试成绩.mdb";
                    OleDbConnection conn = new OleDbConnection(connString);              

                    conn.Open();
                    DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, 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>
            /// 返回某一表的所有字段名
            /// </summary>
            public List<string> GetTableColumn(string tableName)
            {
                string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + @"C:\信息技术考试.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;

                }

            }

  • 相关阅读:
    什么叫“全力以赴”?
    Hibernate 异常 —— Unable to instantiate default tuplize
    也许用得着的英文短语(持续整理)
    也许用得着的英文句子(持续更新)
    iPhone(iOS设备) 无法更新或恢复时, 如何进入恢复模式
    poj 2778 DNA Sequence(AC自动机 + 矩阵快速幂)
    hdu 3974 线段树 将树弄到区间上
    hdu 2865 Polya计数+(矩阵 or 找规律 求C)
    Polya计数
    poj 2888 Magic Bracelet(Polya+矩阵快速幂)
  • 原文地址:https://www.cnblogs.com/moss_tan_jun/p/1794099.html
Copyright © 2011-2022 走看看