private void frm_Drugs_Load(object sender, EventArgs e) { SqlConnection sqlConnection = new SqlConnection(); sqlConnection.ConnectionString = "Server=(local);DataBase=DrugManagement;Integrated Security=sspi"; SqlCommand sqlCommand = sqlConnection.CreateCommand(); sqlCommand.CommandText = //指定SQL命令的命令文本; "SELECT * FROM tb_DrugType;" //该命令分别查询所有院系、专业、班级,查询结果将返回多张表; + "SELECT * FROM tb_DrugFuction;" + "SELECT * FROM tb_Drugs;"; SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); sqlDataAdapter.SelectCommand = sqlCommand; DataSet dataSet = new DataSet(); sqlConnection.Open(); sqlDataAdapter.Fill(dataSet); sqlConnection.Close(); DataTable TypeTable = dataSet.Tables[0]; DataTable FunTable = dataSet.Tables[1]; DataTable drugsTable = dataSet.Tables[2]; DataRelation[] dataRelations = { new DataRelation ("Type_Fun" , TypeTable.Columns["No"] , FunTable.Columns["DrugTypeNo"] , false) , new DataRelation ("Fun_Drugs" , FunTable.Columns["FunNo"] , drugsTable.Columns["DrugsNo"] , false) }; dataSet.Relations.AddRange(dataRelations); this.trv_Drugs.Nodes.Clear(); foreach (DataRow TypeRow in TypeTable.Rows) { TreeNode TypeNode = new TreeNode(); TypeNode.Text = TypeRow["Name"].ToString(); this.trv_Drugs.Nodes.Add(TypeNode); foreach (DataRow FunRow in TypeRow.GetChildRows("Type_Fun")) { TreeNode FunNode = new TreeNode(); FunNode.Text = FunRow["Name"].ToString(); FunNode.Tag = FunRow["FunNo"]; TypeNode.Nodes.Add(FunNode); } } }