我使用這個連接字符串來連接到SQL Server如何填充從SQL Server中的樹狀動態獲得
我試圖填充節點的所有表和列名......但我不能這樣做。
我能夠得到的只是表名而不是子節點(即列名)
connetionString = "Data Source=" + textBox1.Text + ";Initial Catalog=" + comboBox1.Text + ";User ID=" + textBox2.Text + ";Password=" + textBox3.Text;
sql = "SELECT * FROM [sys].[tables]";
connection = new SqlConnection(connetionString);
TreeView mytree = new TreeView();
try
{
connection.Open();
command = new SqlCommand(sql, connection);
adapter.SelectCommand = command;
adapter.Fill(ds, "SQL Temp Table");
adapter.Dispose();
command.Dispose();
connection.Close();
treeView1.Nodes.Clear();
treeView1.Sort();
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
treeView1.Nodes.Add(ds.Tables[0].Rows[i].ItemArray[0].ToString());
for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
{
treeView1.Nodes.Add(ds.Tables[0].Columns[j].ToString());
}
}
}
您需要從'sys.columns'中選擇以獲取列名; 'sys.columns'中的'object_id'對應''sys.tables'中的'object_id' –
感謝您的回覆。讓我試試@marc_s ... –