110,566
社区成员
发帖
与我相关
我的任务
分享
表一:
FriendType
FriendTypeId FriendTypeName
1 我的好友
2 我的同事
3 我的家人
4 陌生人
表2:
Friends
FriendId FriendName FriendTypeId
1 张三 1
2 李四 1
3 王五 2
4 余六 2
5 田七 3
6 朱八 3
7 叶九 4
8 姚个 4
-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
namespace CSDNDemoTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DataSet ds_FriendType = getDataSet("FriendType");
DataSet ds_Friends = getDataSet("Friends");
foreach (DataRow dr in ds_FriendType.Tables[0].Rows)
{
//好友类型表绑定,作为一级层次
TreeNode tn_origine = new TreeNode();
tn_origine.Text = dr["FriendTypeName"].ToString();
this.treeView1.Nodes.Add(tn_origine);
//好友表绑定
DataRow[] dr_arr = ds_Friends.Tables[0].Select("FriendTypeId="+int.Parse(dr["FriendTypeId"].ToString()));
if (dr_arr.Length > 0)
{
foreach (DataRow dr_sub in dr_arr)
{
TreeNode tn_sub = new TreeNode();
tn_sub.Text = dr_sub["FriendName"].ToString();
tn_origine.Nodes.Add(tn_sub);
}
}
}
}
//获取数据集
public DataSet getDataSet(string tableName)
{
DataSet ds = new DataSet();
using (SqlConnection con = new SqlConnection("Data Source=localhost;uid=sa;pwd=saiyang;Database=CSDN"))
{
con.Open();
string strSQL = "select * from "+tableName;
using (SqlDataAdapter sda = new SqlDataAdapter(strSQL, con))
{
sda.Fill(ds);
}
}
return ds;
}
}
}