【问题标题】:Populate treeview with user created tables, and their columns使用用户创建的表及其列填充树视图
【发布时间】:2015-04-28 17:54:13
【问题描述】:

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

我想要做的是:

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

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

代码如下:

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 
...

上述结果的问题在于它还包括非用户创建的表作为父节点,并且我没有从这些表中获取列名(相反,我为每个表获取 TABLE_CATALOG 等)。

问题:

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

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

【问题讨论】:

    标签: c# .net treeview database-schema


    【解决方案1】:

    您的代码中有两个问题。

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

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

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

    示例代码,

    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();
        }
    }
    

    【讨论】:

      【解决方案2】:

      这应该做你想做的事。您可能需要花时间检查表和列 DataTables 中的数据。它们与您可能期望看到的不完全一样。

          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;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-04
        • 1970-01-01
        • 2014-08-23
        • 1970-01-01
        • 1970-01-01
        • 2010-11-12
        相关资源
        最近更新 更多