ADO.Net中DataTable的应用
一、知识点描述
1、概述:DataTable是一个临时保存数据的网格虚拟表(表示内存中数据的一个表),是ADO.Net库中的核心对象。
2、DataTable类将关系数据表示为表格形式,ADO.Net提供了一个DataTable类来独立创建和使用数据表。
3、在创建DataTable之前,必须包含using System.Data名称空间。
4、DataTable的常用属性
①Columns:它用于获取属于此表的列的集合。
②DataSet:它用于获取此表所属的DataSet。
③Rows:它用于获取属于此表的行的集合。
④PrimaryKey:它用于获取或设置一个用作数据表主键的列数组。
⑤TableName:它用于获取或设置DataTable的名称。
5、主要用法:
①创建和使用DataTable
(1)DataTable Table = new DataTable(); (声明实例化数据表)
(2)sqlDataAdapter.Fill(bedTable); (sql数据适配器读取数据,并填充数据表)
(3)this.DataGridView.DataSource =Table; (将数据网格视图的数据源设为数据表)
(4)应用数据表里的各个属性用于绑定数据网格视图的列。
②DataTable中的数据处理
(1)向数据表中插入数据,创建新行并将它们添加到表中。
(2)查看数据表中的数据。
(3)更新或修改数据表中的数据。
(4)删除DataGridView中相关行的数据并同时删除与之绑定的数据表中的数据。
(5)数据表的复制。
二、思维导图
三、示例代码
1、查看数据表中的数据
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString = "Server=(local);Database=EduBase1;Integrated Security=sspi";
SqlCommand sqlCommand = new SqlCommand();
SqlCommand sqlCommand2 = new SqlCommand(); sqlCommand.Connection = sqlConnection; sqlCommand2.Connection = sqlConnection;
sqlCommand.CommandText = "SELECT * FROM tb_Room;"; sqlCommand2.CommandText = "SELECT * FROM tb_Nurse WHER No=@No;"; sqlCommand2.Parameters.AddWithValue("@No", this.工号.Text.Trim()); SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); sqlDataAdapter.SelectCommand = sqlCommand; DataTable roomTable = new DataTable(); sqlConnection.Open(); sqlDataAdapter.Fill(roomTable); this.所属科室.DataSource = roomTable; this.所属科室.DisplayMember = "Name"; this.所属科室.ValueMember = "No"; SqlDataReader sqlDataReader = sqlCommand2.ExecuteReader(); byte[] photoBytes = null; if (sqlDataReader.Read()) { this.姓名.Text = sqlDataReader["Name"].ToString(); this.rdb_Male.Checked = (bool)sqlDataReader["Gender"]; this.rdb_Female.Checked = !(bool)sqlDataReader["Gender"]; this.职称.Text = sqlDataReader["Title"].ToString(); this.所属科室.SelectedValue =(int) sqlDataReader["RoomNo"]; photoBytes =(sqlDataReader["Photo"] == DBNull.Value ? null : (byte[])sqlDataReader["Photo"]); } sqlDataReader.Close(); if (photoBytes !=null) { MemoryStream memoryStream = new MemoryStream(photoBytes); this.照片.Image = Image.FromStream(memoryStream); }
2、更新数据表中的数据
MemoryStream memoryStream = new MemoryStream(); this.照片.Image.Save(memoryStream, ImageFormat.Bmp); byte[] photoBytes = new byte[memoryStream.Length]; memoryStream.Seek(0, SeekOrigin.Begin); memoryStream.Read(photoBytes, 0, photoBytes.Length);
SqlConnection sqlConnection = new SqlConnection(); sqlConnection.ConnectionString "Server=(local);Database=EduBase1;Integrated Security=sspi"; SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText =
"UPDATE tb_Nurse"+ " SET Name=@Name,Gender=@Gender,Title=@Title,RoomNo=@RoomNo,Photo=@Photo" + " WHERE No=@No;"; sqlCommand.Parameters.AddWithValue("@Name", this.姓名.Text.Trim());
sqlCommand.Parameters.AddWithValue("@Gender", this.rdb_Male.Checked); sqlCommand.Parameters.AddWithValue("@Title", this.职称.Text.Trim()); sqlCommand.Parameters.AddWithValue("@RoomNo", (int)this.所属科室.SelectedValue); sqlCommand.Parameters.AddWithValue("@Photo", photoBytes); sqlCommand.Parameters.AddWithValue("@No", this.工号.Text.Trim()); sqlConnection.Open(); int rowAffected = sqlCommand.ExecuteNonQuery(); sqlConnection.Close(); if (rowAffected.ToString() == "1") { MessageBox.Show("更新成功!" ); }
3、删除表中的数据
SqlConnection sqlConnection = new SqlConnection(); sqlConnection.ConnectionString "Server=(local);Database=EduBase1;Integrated Security=sspi"; SqlCommand sqlCommand = new SqlCommand(); sqlCommand.Connection = sqlConnection; sqlConnection.Open(); sqlCommand.CommandText = "DELETE FROM tb_Nurse WHERE No=" + 工号.Text.Trim(); int rowAffected = sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
if (rowAffected.ToString() == "1") { MessageBox.Show("删除成功!"); } else { MessageBox.Show("删除失败!"); }
4、数据复制
①datatable复制表结构:使用.clone()方法; DataTable oldDT = GetDataTable(); DataTable newDT = oldDT.Clone(); ②把datatable中的所有信息复制到一个新的datatable,包括结构和数据: DataTable oldDT = GetDataTable(); DataTable newDT = oldDT.Copy(); ③复制datatable中的某一行:使用.ImportRow()方法; DataTable oldDT = GetDataTable(); DataTable newDT = new DataTable(); newDT.ImportRow(oldDT.Rows[1]);//把原来datatable中的第二行数据复制到新的datatable中。
四、效果截图