用的数据库名称Database,表st_user,需要显示的用户名name
请问为何出错啊?
(附上我复制的后台代码)
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Default15 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
BindDataBase();
TreeView1.ShowLines = true;//显示连接父节点与子节点间的线条
TreeView1.ExpandDepth = 1;//控件显示时所展开的层数
}
public void BindDataBase()
{
//实例化SqlConnection对象
SqlConnection sqlCon = new SqlConnection();
//实例化SqlConnection对象连接数据库的字符串
sqlCon.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf";
//实例化SqlDataAdapter对象
SqlDataAdapter da = new SqlDataAdapter("select * from st_user", sqlCon);
//实例化数据集DataSet
DataSet ds = new DataSet();
da.Fill(ds, "st_user");
}
//下面的方法动态添加了TreeView的根节点和子节点
//设置TreeView的根节点
TreeNode tree1 = new TreeNode("name");
this.TreeView1.Nodes.Add(tree1);
for (int i = 0; i < ds.Tables["st_user"].Rows.Count; i++)
{
TreeNode tree2 = new TreeNode(ds.Tables["st_user"].Rows[i][1].ToString(), ds.Tables["st_user"].Rows[i][1].ToString());
tree1.ChildNodes.Add(tree2);
//显示TreeView根节点下的子节点
for (int j = 0; j < ds.Tables["st_user"].Columns.Count; j++)
{
TreeNode tree3 = new TreeNode(ds.Tables["st_user"].Rows[i][j].ToString(), ds.Tables["st_user"].Rows[i][j].ToString());
tree2.ChildNodes.Add(tree3);
}
}
}