C#读取数据库问题,急
动态树形结构的问题,用的测试数据是随机生成的,如果要从数据库读取该怎么修改呢?
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Data.OracleClient;
namespace DynLoadTree
{
public partial class Default : System.Web.UI.Page
{
#region Create Test Data
private DataTable DtTreeData
{
get
{
if (ViewState["DtTreeData"] == null)
ViewState["DtTreeData"] = GetAllDatas();
return (DataTable)ViewState["DtTreeData"];
}
}
/// <summary>
/// 生成测试数据
/// 实际操作中数据一般从数据库获取
/// </summary>
/// <returns></returns>
private DataTable GetAllDatas()
{
DataTable dt = new DataTable();//创建数据表DataTable对象
dt.Columns.Add("ID", typeof(int));//向Dtable中添加列
dt.Columns.Add("ParentID", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns[0].AutoIncrement = true;
dt.Columns[0].AutoIncrementSeed = 1;
CreateChild(dt, 0, 2);
return dt;
}
catch (Exception ex)
{
Console.WriteLine("打开数据库错误:{0}", ex.Message);//如果发生异常,则提示出错 //如果有错误,输出错误信息
}
&n