大板栗

第九周学习笔记-ADO.Net中DataSet的应用

一、知识点描述

1、含义:DataSetADO.Net的断开式结构的核心组件,它可以用于多种不同的数据源,用于XML数据,或用于管理应用程序本地的数据。DataSet包含一个或多个DataTable对象的集合,这些对象由数据行和数据列以及主键、外键、约束和有关DataTable对象中数据的关系信息组成。

2、ADO.Net中使用DataSet

①创建DataSet

DataSet dataSet = new DataSet();

②向DataSet中添加DataTable

声明数据表1,对应数据集的表集合中的第1张数据表; 

DataTable Table1 = dataSet.Tables[0];

声明数据表2,对应数据集的表集合中的第2张数据表;  

DataTable Table2 = dataSet.Tables[1];

③添加表间关系

在包含多个DataTable的对象的DataSet中,可以使用DataRelation对象来使一个表与另一个表相关,在多个表之间导航,以及从相关表中返回子行或者父行。

④浏览表间关系

DataRelation的一项主要功能就是在DataSet中从一个DataTable浏览到另一个。它使你能够在给定相关DataTable中的单个DataRow的情况下检索一个DataTable中的所有相关DataRow对象。

⑤使用DataSet事件

DataSet提供MergeFailed事件,该事件在所合并的DataSet对象的架构发生冲突时引发。

⑥在DataSet中使用XML

3、DataSet和数据绑定

①简单型数据绑定

TextBox进行数据绑定

②复杂型数据绑定

DataGridView进行数据绑定

 

二、思维导图

 

三、示例代码

1、载入树形图代码

 

SqlConnection sqlConnection = new SqlConnection();            
sqlConnection.ConnectionString = "Server=(local);Database=EduBase1;Integrated Security=sspi"; SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = "SELECT * FROM tb_Room;"+ "SELECT * FROM tb_Doctor;" + "SELECT * FROM tb_Patient;"; SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
sqlDataAdapter.SelectCommand = sqlCommand; DataSet dataSet = new DataSet(); //声明并实例化数据集,用于保存查得的多张表; sqlConnection.Open(); sqlDataAdapter.Fill(dataSet); //SQL数据适配器读取数据,并填充数据集; sqlConnection.Close(); DataTable roomTable = dataSet.Tables[0]; //声明科室数据表,对应数据集的表集合中的第1张数据表; DataTable doctorTable = dataSet.Tables[1];//声明医生数据表,对应数据集的表集合中的第2张数据表; DataTable patientTable = dataSet.Tables[2];//声明病人数据表,对应数据集的表集合中的第1张数据表; DataRelation[] dataRelations = //声明数据关系数组; { new DataRelation ("Room_Doctor" , roomTable.Columns["No"] , doctorTable.Columns["RoomNo"] , false) , new DataRelation ("Doctor_Patient" , doctorTable.Columns["No"] , patientTable.Columns["DoctorNo"] , false) }; dataSet.Relations.AddRange(dataRelations);//将数据关系数组批量加入数据集的关系集合中; this.trv_HospitalUnit.Nodes.Clear(); foreach (DataRow roomRow in roomTable.Rows) { TreeNode roomNode = new TreeNode(); roomNode.Text = roomRow["Name"].ToString(); this.trv_HospitalUnit.Nodes.Add(roomNode); foreach (DataRow doctorRow in roomRow.GetChildRows("Room_Doctor")) { TreeNode doctorNode = new TreeNode(); doctorNode.Text = doctorRow["Name"].ToString(); roomNode.Nodes.Add(doctorNode); foreach (DataRow patientRow in doctorRow.GetChildRows("Doctor_Patient")) { TreeNode patientNode = new TreeNode(); patientNode.Text = patientRow["Name"].ToString(); patientNode.Tag = patientRow["No"]; doctorNode.Nodes.Add(patientNode); } } }

 

2、树形图选中节点事件代码

 

 if (this.trv_HospitalUnit.SelectedNode.Level == 2)       //若树形视图的选中节点的级别为3,即选中病人节点;
            {
                int patientNo = (int)this.trv_HospitalUnit.SelectedNode.Tag;    //将树形视图的选中节点的标签转为整型,即可获得事先保存的病人编号;
                SqlConnection sqlConnection = new SqlConnection();                                          
                sqlConnection.ConnectionString =
                    "Server=(local);Database=EduBase1;Integrated Security=sspi";
                SqlCommand sqlCommand = new SqlCommand(); 
                sqlCommand.Connection = sqlConnection; 
                sqlCommand.CommandText = "SELECT * FROM tb_MedicalAdvice WHERE PatientNo=@PatientNo;";                 
sqlCommand.Parameters.AddWithValue("@PatientNo", patientNo); SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
sqlDataAdapter.SelectCommand = sqlCommand; DataTable medicalAdviceTable = new DataTable(); sqlConnection.Open();
sqlDataAdapter.Fill(medicalAdviceTable); sqlConnection.Close();
this.dgv_Advice.DataSource = medicalAdviceTable;
this.dgv_Advice.Columns["No"].HeaderText = "医嘱"; this.dgv_Advice.Columns["PatientNo"].HeaderText = "病人编号"; this.dgv_Advice.Columns["PatientName"].HeaderText = "病人姓名"; this.dgv_Advice.Columns[this.dgv_Advice.Columns.Count - 1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; }

 

四、效果截图

 

posted on 2018-11-11 20:29  大板栗  阅读(276)  评论(0编辑  收藏  举报

导航