使用用户创建的表及其列填充树视图

Populate treeview with user created tables, and their columns

我正在自学 C# 和 .NET。我正在尝试学习如何使用 GetSchema.

我想做的是:

打开 MS Access 数据库并使用数据库架构填充树视图控件。我想用用户创建的 table 名称填充父节点,它们的子节点将包含列名称。

我尝试修改上面链接的代码示例,但失败了。

代码如下:

using (OleDbConnection OleDBConnection = new OleDbConnection())
{
    OleDBConnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + 
        "Data Source=" + databaseLocation + ";Persist Security Info=False;";

    try
    {
        OleDBConnection.Open();
        // get database schema
        DataTable dataTable = OleDBConnection.GetSchema("Tables");
        // clear the treeview
        TreeView.Nodes.Clear();
        // here I tried to populate the treeview but have failed
        foreach (System.Data.DataRow row in dataTable.Rows)
        {
            // add parent node -> table name
            TreeView.Nodes.Add(row["TABLE_NAME"].ToString());
            // now add children -> all table columns
            foreach(DataColumn column in dataTable.Columns)
            {
                TreeView.Nodes.Add(column.ColumnName);
            }
        }
        // all done, close the connection
        OleDBConnection.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
        Application.Exit();
    }
}

我在 Northwind 数据库上测试时得到的结果如下:

- Customers
    - TABLE_CATALOG
    - TABLE_SCHEMA
    - TABLE_NAME
    - TABLE_TYPE
    - TABLE_GUID
    - DESCRIPTION
    - TABLE_PROPID
    - DATE_CREATED
    - DATE_MODIFIED
- Employees
    - TABLE_CATALOG
    - TABLE_SCHEMA
    - TABLE_NAME
    - TABLE_TYPE
    - TABLE_GUID
    - DESCRIPTION
    - TABLE_PROPID
    - DATE_CREATED
    - DATE_MODIFIED 
...

上述结果的问题是它还包括非用户创建的 tables 作为父节点,我没有从那些 tables 中获取列名(相反,我得到TABLE_CATALOG 依此类推 table).

问题:

如何加载用户创建的 tables 作为父节点,并添加包含这些 tables 列名称的子节点?

再次,如果解决方案很简单,我深表歉意,但请记住,这是我的第一次尝试,因为我刚刚开始使用 C# 和 .NET

这应该可以满足您的要求。您可能需要花时间检查 table 和列数据表中的数据。它们并不完全像您期望看到的那样。

    public static TreeView PopulateTreeViewWithSchema(System.Data.OleDb.OleDbConnection conn, TreeView tree)
    {
        if (tree == null)
            tree = new TreeView();

        // get database schema
        DataTable tableTable = conn.GetSchema("Tables");
        DataTable columnsTable = conn.GetSchema("Columns");

        // clear the treeview
        tree.Nodes.Clear();
        // here I tried to populate the treeview but have failed
        foreach (System.Data.DataRow row in tableTable.Rows)
        {
            // add parent node -> table name
            string tableName = row["TABLE_NAME"].ToString();
            if (!tableName.StartsWith("MSys"))
            {
                TreeNode node = new TreeNode(tableName);
                tree.Nodes.Add(node);
                // now add children -> all table columns
                foreach (System.Data.DataRow columnRow in columnsTable.Rows)
                {
                    if (columnRow["TABLE_NAME"].ToString().Equals(tableName))
                    {
                        node.Nodes.Add(columnRow["COLUMN_NAME"].ToString());
                    }
                }
            }
        }
        return tree;
    }

您的代码中有两个问题。

  1. GetSchema returns 仅table 数据库的详细信息。那
    信息不包含列详细信息。过滤掉系统
    tables 您必须使用 GetSchema() 的其他重载。你可以
    指定 table 类型的过滤条件。 "TABLE"中的限制 引用用户创建的 tables。其他值为“SYSTEM TABLE, ACCESS
    TABLE, TABLE".

  2. 应将列添加为父树节点的子节点。

建议:使用调试可视化工具来识别 table 中可用的数据。

示例代码,

using (OleDbConnection OleDBConnection = new OleDbConnection())
{
    OleDBConnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
        "Data Source=" + databaseLocation + ";Persist Security Info=False;";

    try
    {
        OleDBConnection.Open();

        string[] restrictions2 = new string[] { null, null, null, "TABLE" };
        System.Data.DataTable DataTable2 = OleDBConnection.GetSchema("Tables", restrictions2);

        // clear the treeview
        treeView1.Nodes.Clear();
        // here I tried to populate the treeview but have failed
        foreach (System.Data.DataRow row in DataTable2.Rows)
        {
            // add parent node -> table name
            var parent = treeView1.Nodes.Add(row["TABLE_NAME"].ToString());
            // now add children -> all table columns

            string[] restrictions1 = new string[] { null, null, (string)row["TABLE_NAME"], null };
            System.Data.DataTable DataTable1 = OleDBConnection.GetSchema("Columns", restrictions1);

            foreach (DataRow row1 in DataTable1.Rows)
            {
                parent.Nodes.Add((string)row1["COLUMN_NAME"]);
            }
        }
        // all done, close the connection
        OleDBConnection.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
        Application.Exit();
    }
}