![Csharp Csharp](http://www.spersky.com/logo/Csharp.gif)
这个例子主要是展示数据库是Access时的 dataset操作,与数据是sql是有些不同,相对来说更加简洁;具体的dataset操作是在代码最后部分。
/// <summary>
/// 绑定顶级分类
/// </summary>
private void BindDrpClass()
{
DataTable dt = GetClassList("").Tables[0];
ddlLevel.Items.Clear();
ddlLevel.Items.Add(new ListItem("添加根栏目", "0"));
DataRow[] drs = dt.Select("sParent= " + 0);
foreach (DataRow dr in drs)
{
string classid = dr["ID"].ToString();
string classname = dr["sName"].ToString();
//顶级分类显示形式
classname = "◆" + classname;
ddlLevel.Items.Add(new ListItem(classname, classid));
int sonparentid = int.Parse(classid);
string blank = "├";//
//递归子分类方法
BindDrpNode(sonparentid, dt, blank);
}
ddlLevel.DataBind();
}
/// <summary>
/// 绑定子分类
/// </summary>
/// <param name="parentid"></param>
/// <param name="dt"></param>
/// <param name="blank"></param>
private void BindDrpNode(int parentid, DataTable dt, string blank)
{
DataRow[] drs = dt.Select("sParent= " + parentid);
foreach (DataRow dr in drs)
{
string classid = dr["ID"].ToString();
string classname = dr["sName"].ToString();
classname = blank + classname;
ddlLevel.Items.Add(new ListItem(classname, classid));
int sonparentid = int.Parse(classid);
string blank2 = blank + "─";
BindDrpNode(sonparentid, dt, blank2);
}
}
/// <summary>
/// 数据访问
/// </summary>
/// <param name="strWhere">SQL条件</param>
/// <returns></returns>
public DataSet GetClassList(string strWhere)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select * from Sort ");
//string strSql = "select * from Sort ";
if (strWhere.Trim() != "")
{
strSql.Append(" where " + strWhere);
}
//return SqlHelper.ExecuteDataset(SqlHelper.strConn, CommandType.Text, strSql.ToString()); //这个为sql数据库方法(需要sqlhelper对应语句支持)
#region ForAccess //适用于对Access的dataset操作
//创建DataSet对象
OleDbDataAdapter da = new OleDbDataAdapter(strSql.ToString(), SqlHelper.strConn); //引用数据库连接dbconn并依据SQL语句"select * from kaizhi"创建OleDbDataAdapter对象da
DataSet ds = new DataSet(); //创建DataSet对象
da.Fill(ds); //用OleDbDataAdapter对象da填充、更新刚创建的DataSet对象
return ds;
#endregion ForAccess
}