TreeView连接数据
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source='" + HttpContext.Current.Server.MapPath("~/LHB_DataBase/rss.mdb") + "';Jet OLEDB:Engine Type=5;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False";
OleDbConnection cn = new OleDbConnection();
OleDbCommand cmd = new OleDbCommand();
private void TreeViewOpeartor()
{
this.TreeView1.Nodes.Clear();
string OledbString = "select * from categories ";
DataTable rss_Url = this.GetRss_Url();
cn.ConnectionString = connectionString;
cn.Open();
cmd.Connection = cn;
cmd.CommandText = OledbString;
OleDbDataReader dda = cmd.ExecuteReader();
while(dda.Read())
{
TreeNode theNode = new TreeNode ();
theNode.Text = dda["categoryname"].ToString();
theNode.NodeData = dda["ID"].ToString();
this.TreeView1.Nodes.Add(theNode);
DataRow[] urls = rss_Url.Select("categoryID=" + dda["ID"].ToString());
for(int i = 0 ; i < urls.Length ; i++)
{
Microsoft.Web.UI.WebControls.TreeNode node = new Microsoft.Web.UI.WebControls.TreeNode();
node.Text = urls[i]["Rss_name"].ToString();
node.Target = urls[i]["Id"].ToString();
node.NodeData = urls[i]["ID"].ToString();
theNode.Nodes.Add(node);
}
}
dda.Close();
cn.Close();
}
private DataTable GetRss_Url()
{
cn.ConnectionString = connectionString ;
OleDbDataAdapter dda = new OleDbDataAdapter("select * from rss where releasetoweb=1 ",cn);
DataTable dt = new DataTable();
dda.Fill(dt);
return dt;
}