-1
我試圖創建一個層次結構樹視圖,其中列出了我的SQL Server名稱作爲根,所有數據庫,每個數據庫中的所有表,存儲過程使用C#。我努力讓表匹配他們的數據庫。我還是C#的新手 Here is an example of what I want my result to look like如何在C#中創建列出SQL數據的層次結構列表視圖
我試圖創建一個層次結構樹視圖,其中列出了我的SQL Server名稱作爲根,所有數據庫,每個數據庫中的所有表,存儲過程使用C#。我努力讓表匹配他們的數據庫。我還是C#的新手 Here is an example of what I want my result to look like如何在C#中創建列出SQL數據的層次結構列表視圖
我沒有得到任何錯誤,我現在最大的問題是讓我的查詢工作。我有一個查詢完全給我我需要的東西,並且該查詢目前在我的代碼中,但它在C#中不起作用。以下是我的代碼。
HTML
<div class="Demo">
<h1>Tree Demo</h1>
<div class="tree well">
<ul>
<li><span>ServerName</span>
<ul>
<asp:Repeater ID="rpDatabasesParent" runat="server" OnItemDataBound="loadTables">
<ItemTemplate>
<li><span><%# Eval("Name") %></span>
<ul>
<h3>Tables</h3>
<asp:Repeater ID="rpTablesChild" runat="server" OnItemDataBound="loadTables">
<ItemTemplate>
<li><span><%# Eval("Tables") %></span></li>
</ItemTemplate>
</asp:Repeater>
</ul>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
</li>
</ul>
</div>
</div>
C#
protected void Page_Load(object sender, EventArgs e)
{
testConnection();
}
public void testConnection()
{
using (SqlConnection connection = new SqlConnection(masterConnectionString))
{
try
{
connection.Open();
SqlCommand cmd = new SqlCommand("SELECT Name From Sys.Databases", connection);
cmd.CommandType = CommandType.Text;
SqlDataReader rdr = cmd.ExecuteReader();
rpDatabasesParent.DataSource = rdr;
rpDatabasesParent.DataBind();
}
catch (Exception err)
{
//log something
}
finally
{
connection.Close();
}
}
}
protected void loadTables(object sender, RepeaterItemEventArgs e)
{
RepeaterItem item = e.Item;
Repeater rpTablesChild = (Repeater)item.FindControl("rpTablesChild");
using (SqlConnection connection = new SqlConnection(masterConnectionString))
{
try
{
connection.Open();
SqlCommand cmd = new SqlCommand("DECLARE @SERVERNAME SYSNAME = @@SERVERNAME, @DB SYSNAME, @Object SYSNAME, @Str NVARCHAR(4000); DECLARE DBList CURSOR LOCAL FAST_FORWARDFOR SELECT name FROM sys.databases; OPEN DBList; FETCH NEXT FROM DBList INTO @DB; WHILE @@FETCH_STATUS = 0 BEGINSELECT @Str = 'SELECT name AS Tables FROM '[email protected]+'.sys.tables; SELECT TABLE_NAME AS Views FROM '[email protected]+'.INFORMATION_SCHEMA.VIEWS; SELECT ROUTINE_NAME AS StoredProcedures FROM '[email protected]+'.INFORMATION_SCHEMA.ROUTINES;' EXEC sp_executesql @StrFETCH NEXT FROM DBList INTO @DB; ENDCLOSE DBList; DEALLOCATE DBList;", connection);
cmd.CommandType = CommandType.Text;
SqlDataReader rdr = cmd.ExecuteReader();
rpTablesChild.DataSource = rdr;
rpTablesChild.DataBind();
}
catch (Exception err)
{
//log something
}
finally
{
connection.Close();
}
}
}
這不是你問什麼的,但可能會幫助您開始在正確的方向...從那裏你可以試試你的自我,然後回來可能有的具體問題。 https://stackoverflow.com/questions/13703193/how-to-get-list-of-all-database-from-sql-server-in-a-combobox-using-c-net – 0perator
WWW.CodeProjects..Com有很多很好的例子。嘗試以下谷歌搜索:codeproject c#自定義列表視圖 – jdweng
如果你掙扎,這聽起來像你已經做了一個嘗試。請發佈你的代碼和你得到的錯誤。 –