分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

ADO.Net中DataSete的应用

发布时间:2023-09-06 02:21责任编辑:傅花花关键词:暂无标签

1.思维导图

2.知识点描述

(1)介绍

   DataSet可以看做是一个内存中的数据库,包括表、数据行、数据列以及表与表之间 的关系。创建一个DataSet后,它就可以单独存在,不需要一直保持和数据库的连接。应用程序需要数据时,可以直接从内存中的DataSet中读取数据。

   DataSet可以包含多个表,这些表构成了数据表集合DataTableCollection,其中的每个表都是一个DataTable对象,每个表又包括数据行DataRow和列DataColumn,所有的行构成数据行集合DataRowCollection,所有的列构成数据列集合DataColumnCollection。DataSet还可以包含表与表之间的关系DataRelation。DataRelation表示数据集中DataTable之间的关系。可以用来保证执行完整性约束、数据级联。

(2)创建DataSet对象:

DataSet dataset=new DataSet();

(3)通过DataAdapter对象存储数据

SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();sqlDataAdapter.SelectCommand = sqlCommand;DataSet dataSet = new DataSet();sqlConnection.Open();sqlDataAdapter.Fill(dataSet);sqlConnection.Close();

3.应用举例

 树形图(使用treeview控件分级并显示数据)

(1)连接数据库

SqlConnection sqlConnection = new SqlConnection(); ?????sqlConnection.ConnectionString = ???????????"Server=(local);Database=DB_Equipment;Integrated Security=sspi"; ????????SqlCommand sqlCommand = new SqlCommand(); ????????sqlCommand.Connection = sqlConnection; ????????sqlCommand.CommandText= ???????????????"SELECT * FROM tb_classifyone;" ???????????????+ "SELECT * FROM tb_classifytwo;" ???????????????+ "SELECT * FROM tb_classifythree;";

(2)声明并实例化数据集,用于保存查得的多张表

SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); ???????????sqlDataAdapter.SelectCommand = sqlCommand; ???????????DataSet dataSet = new DataSet(); ???????????sqlConnection.Open(); ???????????sqlDataAdapter.Fill(dataSet); ???????????sqlConnection.Close(); ???????????DataTable classifyoneTable = dataSet.Tables[0]; ???????????DataTable classifytwoTable = dataSet.Tables[1]; ???????????DataTable classifythreeTable = dataSet.Tables[2];

(3)设置分级关系(节点)

DataRelation[] dataRelations = ???????????{ ???????????????new DataRelation ????????????????????("classifyone_classifytwo" ???????????????????, classifyoneTable.Columns["No"] ???????????????????, classifytwoTable.Columns["ClassifyoneNo"] ???????????????????, false) ???????????????, new DataRelation ???????????????????("classifytwo_classifythree" ???????????????????, classifytwoTable.Columns["No"] ???????????????????, classifythreeTable.Columns["ClassifytwoNo"] ???????????????????, false) ???????????}; ???????????dataSet.Relations.AddRange(dataRelations); ???????????foreach (DataRow classifyoneRow in classifyoneTable.Rows) ???????????{ ???????????????TreeNode classifyoneNode = new TreeNode(); ???????????????classifyoneNode.Text = classifyoneRow["Name"].ToString(); ???????????????classifyoneNode.Tag = classifyoneRow["No"]; ???????????????this.trv_classify.Nodes.Add(classifyoneNode); ???????????????foreach (DataRow classifytwoRow in classifyoneRow.GetChildRows("classifyone_classifytwo")) ???????????????{ ???????????????????TreeNode classifytwoNode = new TreeNode(); ???????????????????classifytwoNode.Text = classifytwoRow["Name"].ToString(); ???????????????????classifytwoNode.Tag = classifytwoRow["No"]; ???????????????????classifyoneNode.Nodes.Add(classifytwoNode); ???????????????????foreach (DataRow classifythreeRow in classifytwoRow.GetChildRows("classifytwo_classifythree")) ???????????????????{ ???????????????????????TreeNode classifythreeNode = new TreeNode(); ???????????????????????classifythreeNode.Text = classifythreeRow["Name"].ToString(); ???????????????????????classifythreeNode.Tag = classifythreeRow["No"]; ???????????????????????classifytwoNode.Nodes.Add(classifythreeNode); ???????????????????} ???????????????} ???????????}

(4)读取数据

if (this.trv_classify.SelectedNode.Level == 2) ???????????{ ???????????????int ClassifythreeNo = (int)this.trv_classify.SelectedNode.Tag; ???????????????SqlConnection sqlConnection = new SqlConnection(); ???????????????sqlConnection.ConnectionString = ???????????????????"Server=(local);Database=DB_Equipment;Integrated Security=sspi"; ???????????????SqlCommand sqlCommand = new SqlCommand(); ???????????????sqlCommand.Connection = sqlConnection; ???????????????sqlCommand.CommandText = "SELECT EquipmentNo,EquipmentName,Quantity FROM tb_inventory WHERE ClassifythreeNo=@ClassifythreeNo;"; ???????????????sqlCommand.Parameters.AddWithValue("@ClassifythreeNo", ClassifythreeNo); ???????????????SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); ???????????????sqlDataAdapter.SelectCommand = sqlCommand; ???????????????DataTable inventoryTable = new DataTable(); ???????????????sqlConnection.Open(); ???????????????sqlDataAdapter.Fill(inventoryTable); ???????????????sqlConnection.Close(); ???????????????this.dgv_inventory.DataSource = inventoryTable; ???????????????this.dgv_inventory.Columns[this.dgv_inventory.Columns.Count - 1].AutoSizeMode = ???????????????????DataGridViewAutoSizeColumnMode.Fill; ???????????}

 运行结果图

ADO.Net中DataSete的应用

原文地址:https://www.cnblogs.com/JIALIH-H/p/9926145.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved