public static void LoadTree( TreeView tree)
{
string cnnString = "workstation id=GHAFFARI;packet size=4096;integrated security=SSPI;initial catalog=TempDataBase;persist security info=False";
SqlConnection con = new SqlConnection( cnnString );
DataSet ds = new DataSet();
if( con.State != ConnectionState.Open )
con.Open();
SqlCommand cmd = null;
SqlDataAdapter adapter = null;
cmd = new SqlCommand( "select * from t_books", con );
adapter = new SqlDataAdapter( cmd );
adapter.Fill( ds, "t_books" );
cmd = new SqlCommand( "select * from t_sessions", con );
adapter = new SqlDataAdapter( cmd );
adapter.Fill(ds, "t_sessions");
cmd = new SqlCommand( "select * from t_titles", con );
adapter = new SqlDataAdapter( cmd );
adapter.Fill(ds, "t_titles");
TreeNode book;
TreeNode session;
TreeNode title;
foreach( DataRow row in ds.Tables["t_books"].Rows )
{
book = new TreeNode( (string)row["bookName"] );
book.Tag = row;
ds.Tables["t_sessions"].DefaultView.RowFilter = "bookID=" + row["bookID"];
for (int i = 0; i < ds.Tables["t_sessions"].DefaultView.Count; i++)
{
DataRowView sessionRow = ds.Tables["t_sessions"].DefaultView[i];
session = new TreeNode( (string)sessionRow["sessionName"] );
session.Tag = sessionRow;
ds.Tables["t_titles"].DefaultView.RowFilter = "sessionID=" + sessionRow["sessionID"];
for (int j = 0; j < ds.Tables["t_titles"].DefaultView.Count; j++)
{
DataRowView titleRow = ds.Tables["t_titles"].DefaultView[j];
title = new TreeNode( (string)titleRow["titleName"] );
title.Tag = titleRow;
session.Nodes.Add(title);
}
book.Nodes.Add(session);
}
tree.Nodes.Add(book);
}
}
------ SQL Script
CREATE TABLE [dbo].[t_books] (
[bookID] [numeric](18, 0) IDENTITY (1, 1) NOT NULL ,
[BookName] [nvarchar] (50) COLLATE Arabic_CI_AS NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[t_sessions] (
[SessionID] [numeric](18, 0) IDENTITY (1, 1) NOT NULL ,
[SessionName] [nvarchar] (50) COLLATE Arabic_CI_AS NULL ,
[BookID] [numeric](18, 0) NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[t_titles] (
[titleID] [numeric](18, 0) IDENTITY (1, 1) NOT NULL ,
[titleName] [nvarchar] (50) COLLATE Arabic_CI_AS NULL ,
[sessionID] [numeric](18, 0) NULL
) ON [PRIMARY]
GO
راستش مشکل کد شما رومتوجه نشدهم، ولی این کد ( کد تغییر داده شه کد شماست ) درست اجرا شد. شما هم تستش کن.