标签:treenode setname 导图 local lib set net add fill
一、知识描述
1、什么是DataSet?
DataSet为一个数据集,可以看做是一个内存中的数据库,包括表、数据行、数据列以及表与表之间的关系。
创建一个DataSet后,它可以单独存在,不需要一直保持和数据库的连接。
2、DataSet的使用方法:
(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 KESHI=@KESHI;"; 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; }
三、效果截图
四、思维导图
标签:treenode setname 导图 local lib set net add fill
原文地址:https://www.cnblogs.com/dearzy35/p/9998105.html