日期:2014-05-18 浏览次数:21029 次
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试验数据"); TreeNode[] nodes; Func<TreeNode, string, TreeNode> func = (v1, v2) => (nodes = v1.Nodes.Find(v2, false)).Length > 0 ? nodes[0] : v1.Nodes.Add(v2); while (reader.Read()) { var num = reader.GetString(0); var category = reader.GetString(1); var model = reader.GetString(2); var date = reader.GetString(3); var year = date.Substring(0, 4) + "年"; 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();