DataSet的应用

Posted stanzhou47

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DataSet的应用相关的知识,希望对你有一定的参考价值。

DataSet的应用

 

一、知识描述

 

1、什么是DataSet?

DataSet为一个数据集,可以看做是一个内存中的数据库,包括表、数据行、数据列以及表与表之间的关系。

创建一个DataSet后,它可以单独存在,不需要一直保持和数据库的连接。

 

 

 

2DataSet的使用方法:

1)创建DataSet对象

DataSet ds = new DataSet();

DataSet ds = new DataSet(DataSetName);

3)调用SqlDataAdapter对象的Fill方法向数据集中填充数据

SqlDataAdapter da = new SqlDataAdapter(sql, conn);

DataSet ds = new DataSet();

da.Fill(ds);

 

4)如果DataSet中已包含数据,可以通过如下代码访问第一个表中的第i行第j列的数据(索引从0开始)

ds.Tables[0].Rows[i].ItemArray[j];

或者

ds.Tables[0].Rows.Count;

 

3、DataSet的基本结构:

技术分享图片

 

 

二、实例代码

提取数据到树结点:

SqlConnection sqlConnection = new SqlConnection();                                              

            sqlConnection.ConnectionString =

                "Server=(local);Database=Message;Integrated Security=sspi";                     

            SqlCommand sqlCommand = new SqlCommand();                                                       

            sqlCommand.Connection = sqlConnection;                                                          

            sqlCommand.CommandText =                                                                        

                "SELECT * FROM KESHI;";                                                           

            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                           

            sqlDataAdapter.SelectCommand = sqlCommand;                                                      

            DataSet dataSet = new DataSet();                                                                

            sqlConnection.Open();                                                                           

            sqlDataAdapter.Fill(dataSet);                                                                   

            sqlConnection.Close();                                                                          

            DataTable keshiTable = dataSet.Tables[0];                                                  

            this.trv_keshi.Nodes.Clear();                                                           

            foreach (DataRow keshiRow in keshiTable.Rows)                                         

            {

                TreeNode keshiNode = new TreeNode();                                                   

                keshiNode.Text = keshiRow["Name"].ToString();                                     

                this.trv_keshi.Nodes.Add(keshiNode);                                                         

           }

 

 

 

提取数据到数据集:

 if (this.trv_keshi.SelectedNode.Level == 0)                                             

            {

        int KESHI = (int)this.trv_keshi.SelectedNode.Tag;                                             SqlConnection sqlConnection = new SqlConnection();                                          

        sqlConnection.ConnectionString =

            "Server=(local);Database=Message;Integrated Security=sspi";                     

           SqlCommand sqlCommand = new SqlCommand();                                                   

           sqlCommand.Connection = sqlConnection;                                                      

           sqlCommand.CommandText = "SELECT D_ID,DOCTOR FROM DMESSAGE WHERE [email protected];";                  sqlCommand.Parameters.AddWithValue("@KESHI", KESHI);                               

           SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                       

           sqlDataAdapter.SelectCommand = sqlCommand;                                                  

           DataTable keshiTable = new DataTable();                                                  

           sqlConnection.Open();                                                                       

           sqlDataAdapter.Fill(keshiTable);                                                          

           sqlConnection.Close();                                                                      

           this.dgv_keshi.DataSource =keshiTable;                                                 

           this.dgv_keshi.Columns["D_I"].HeaderText = "工号";                                         

           this.dgv_keshi.Columns["DOCTOR"].HeaderText = "姓名";

           this.dgv_keshi.Columns[this.dgv_keshi.Columns.Count - 1].AutoSizeMode =                   

               DataGridViewAutoSizeColumnMode.Fill;

            }

三、效果截图

技术分享图片

 

以上是关于DataSet的应用的主要内容,如果未能解决你的问题,请参考以下文章

如何在 SqlDataAdapter/DataSet 上应用 ReliableSqlConnection 的重试策略

如何将自定义函数应用于 xarray.Dataset 坐标的每个值?

`filter`/`where` 有条件地应用到 Spark `Dataset`/`Dataframe`

[WinForm] - "更新 DataSet 应用程序集对象失败,Visual Studio 自动重启" 之解决

在 Apache Spark Dataset<Row> 上应用 flatMap 操作时出现意外的编码器行为

关于DataSet事务处理以及SqlDataAdapter四种用法