码迷,mamicode.com
首页 > Web开发 > 详细

ADO.Net中DataSete的应用

时间:2018-11-08 19:09:40      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:Nero   from   tostring   ant   connect   new   ddr   col   ons   

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的应用

标签:Nero   from   tostring   ant   connect   new   ddr   col   ons   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!