gpt4 book ai didi

c# - 如何使用层次结构级别填充 TreeViewControl?

转载 作者:行者123 更新时间:2023-11-30 18:07:07 25 4
gpt4 key购买 nike

我有一个 TreeView 控件(应该是这样的)

alt text

但我不知道如何用我的查询填充它:

SELECT T.TableName, C.Column_Name FROM Information_Schema.Tables T
INNER JOIN Information_Schema.Columns C
ON T.TableName= C.TableName
WHERE T.TableName IN('BASE_TABLE', 'BASE TABLE')
ORDER BY 1, C.Ordinal_Position

谁能帮帮我...

谢谢。

编辑这是我尝试过的,但只是表名...

private void PopulateTreeView()
{
SqlCeCommand cmd = new SqlCeCommand();
try
{
using (SqlCeConnection conn = new SqlCeConnection("Data Source=" + connString))
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES";
conn.Open();
cmd.Connection = conn;
cmd.ExecuteNonQuery();
// Don't know what's next...
}
}
catch (Exception x)
{
MessageBox.Show(x.GetBaseException().ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
cmd.Dispose();
}
}

最佳答案

这是一个做类似事情的非 LINQ 答案:

using (var conn = new SqlCeConnection(connectionString))
using (var cmd = conn.CreateCommand())
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = @"
SELECT T.TABLE_NAME, C.COLUMN_NAME
FROM INFORMATION_SCHEMA.TABLES T
INNER JOIN INFORMATION_SCHEMA.COLUMNS C
ON T.TABLE_NAME= C.TABLE_NAME
WHERE T.TABLE_NAME IN('BASE_TABLE', 'BASE TABLE')
ORDER BY 1, C.ORDINAL_POSITION";
conn.Open();
cmd.Connection = conn;
using (var reader = cmd.ExecuteReader())
{
string lastTable = null;
TreeNode tableNode = null;
while (reader.Read()) {
if (lastTable != reader.GetString(0)) {
lastTable = reader.GetString(0);
tableNode = new TreeNode(lastTable);
myTree.Nodes.Add(tableNode);
}
tableNode.ChildNodes.Add(new TreeNode(reader.GetString(1)));
}
}
}

关于c# - 如何使用层次结构级别填充 TreeViewControl?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4242856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com