日期:2014-05-20 浏览次数:20854 次
var conn = new SqlConnection("......");
conn.Open();
var cmd = conn.CreateCommand();
cmd.CommandText = "select 电机编号, 电机类别, 电机型号, 试验日期 from 表";
var reader = cmd.ExecuteReader();
var rootNode = this.treeView1.Nodes.Add("车用电机NVH试验数据");
Func<TreeNode, string, TreeNode> func = (v1, v2) =>
{
TreeNode[] nodes = v1.Nodes.Find(v2, false);
if (nodes.Length > 0)
{
return nodes[0];
}
TreeNode n = v1.Nodes.Add(v2);
n.Name = v2;
return n;
};
while (reader.Read())
{
var num = reader.GetString(0);
var category = reader.GetString(1);
var model = reader.GetString(2);
var year = reader.GetDateTime(3).ToString("yyyy") + "年";
var cateNode = func(rootNode, category);
var modelNode = func(cateNode, model);
var yearNode = func(modelNode, year);
yearNode.Nodes.Add(num);
}
reader.Dispose();
cmd.Dispose();
conn.Dispose();