ADO.NET中DataTable的应用

 

知识点

DataTable是一个临时保存数据的网格虚拟表(表示内存中数据的一个表。)

1.用法介绍:

(1)可以使用相应的 DataTable 构造函数创建 DataTable 对象。 可以通过使用 Add 方法将其添加到 DataTable 对象的 Tables集合中,将其添加到 DataSet 中。

(2)使用 DataAdapter 对象的 Fill 方法或 FillSchema 方法在 DataSet 中创建,或者使用 DataSet 的 ReadXml、ReadXmlSchema 或InferXmlSchema 方法从预定义的或推断的 XML 架构中创建。 请注意,将一个 DataTable 作为成员添加到一个 DataSet 的 Tables 集合中后,不能再将其添加到任何其他 DataSet 的表集合中。

(3)在为 DataTable 定义了架构之后,可通过将 DataRow 对象添加到表的 Rows 集合中来将数据行添加到表中。

2.在DataTable中处理数据

(1)向数据表中添加数据

(2)说明如何创建新行并将它们添加到表中。

(3)查看数据表中的数据

(4)说明如何访问行中的数据,包括数据的原始版本和当前版本。

(5)Load 方法

(6)说明如何通过 Load 方法使用行填充 DataTable。

(7)DataTable 编辑

(8)说明如何修改行中的数据,包括挂起对行的更改,直至验证并接受了建议的更改。

(9)行状态与行版本

(10)提供有关行的不同状态的信息。

(11)DataRow 删除

(12)说明如何从表中移除行。

(13)行错误信息

(14)说明如何插入每行的错误信息,帮助解决应用程序中的数据问题。

(15)AcceptChanges 和 RejectChanges

(16)说明如何接受或拒绝对行的更改。

3.属性和方法

(1)属性:

名称 说明
CaseSensitive 指示表中的字符串比较是否区分大小写
Columns 获取属于该表的列的集合。
DataSet 获取此表所属的 DataSet。
HasErrors 获取一个值,该值指示该表所属的 DataSet 的任何表的任何行中是否有错误。
Rows 获取属于该表的行的集合。
TableName 获取或设置 DataTable 的名称

 

 

 

 

(2)方法:

名称 方法
Clear 清除所有数据的 DataTable。
Clone 克隆 DataTable 的结构,包括所有 DataTable 架构和约束。
Compute 计算用来传递筛选条件的当前行上的给定表达式
Copy 复制该 DataTable 的结构和数据。
GetErrors 获取包含错误的 DataRow 对象的数组。
GetType 获取当前实例的 Type。 (继承自 Object。)
ImportRow 将 DataRow 复制到 DataTable 中,保留任何属性设置以及初始值和当前值

 

 

 

 

 

示例代码和效果截图

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
namespace VS
{
    public partial class Shoufeixiangmu : Form
    {
        private DataTable CourseTable;
        private DataView CourseViewByName;
        public Shoufeixiangmu()
        {
            InitializeComponent();
        }

        private void dataGridView1_BindingContextChanged(object sender, EventArgs e)
        {
            SqlConnection sqlConnection = new SqlConnection();                                              
            sqlConnection.ConnectionString =
            ConfigurationManager.ConnectionStrings["Sql"].ConnectionString;                       

            SqlCommand sqlCommand2 = new SqlCommand();                                                      

            sqlCommand2.Connection = sqlConnection;                                                         

            sqlCommand2.CommandText = "SELECT * FROM Shoufeixiangmu;";                                          


            SqlDataAdapter sqlDataAdapter2 = new SqlDataAdapter();                                         
            sqlDataAdapter2.SelectCommand = sqlCommand2;                                                    
            DataTable studentTable = new DataTable();                                                       
            sqlConnection.Open();                                                                           
            //SQL数据适配器读取数据,并填充班级数据表;
            sqlDataAdapter2.Fill(studentTable);                                                             
            sqlConnection.Close();                                                                          
            this.dgv_Score.Columns.Clear();                                                                 
            this.dgv_Score.DataSource = studentTable;                                                       

            this.dgv_Score.Columns[this.dgv_Score.Columns.Count - 1].AutoSizeMode =                         
                DataGridViewAutoSizeColumnMode.Fill;


        }



        private void dgv_Score_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {

            if (this.dgv_Score.SelectionMode != DataGridViewSelectionMode.FullColumnSelect)
            {
                
                textBox1.Text = dgv_Score.Rows[e.RowIndex].Cells[0].Value.ToString();
                textBox2.Text = dgv_Score.Rows[e.RowIndex].Cells[1].Value.ToString();
                textBox3.Text = dgv_Score.Rows[e.RowIndex].Cells[2].Value.ToString();
                textBox4.Text = dgv_Score.Rows[e.RowIndex].Cells[3].Value.ToString();
                textBox5.Text = dgv_Score.Rows[e.RowIndex].Cells[4].Value.ToString();
                textBox6.Text = dgv_Score.Rows[e.RowIndex].Cells[5].Value.ToString();
                textBox7.Text = dgv_Score.Rows[e.RowIndex].Cells[6].Value.ToString();
                
            }
        }

        private void dgv_Score_SelectionChanged(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox8_TextChanged(object sender, EventArgs e)
        {
            SqlConnection sqlConnection = new SqlConnection();                                              
            sqlConnection.ConnectionString =
                "Server=(local);Database=EduBase2018;Integrated Security=sspi";                             
            SqlCommand sqlCommand = new SqlCommand();                                                       
            sqlCommand.Connection = sqlConnection;                                                          
            sqlCommand.CommandText = "SELECT * FROM Shoufeixiangmu;";
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                           
            sqlDataAdapter.SelectCommand = sqlCommand;                                                      
            sqlDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;                            
            this.CourseTable = new DataTable();                                                             
            sqlConnection.Open();                                                                           
            sqlDataAdapter.Fill(this.CourseTable);                                                         
            
            sqlConnection.Close();                                                                          
                                                             
            this.CourseViewByName = new DataView();                                                         
            this.CourseViewByName.Table = this.CourseTable;                                                 
            this.CourseViewByName.Sort = "编号 ASC";  
            DataRow[] searchResultRows =
               this.CourseTable.Select("拼音码 LIKE '%" + this.textBox8.Text.Trim() + "%'");             
            DataTable searchResultTable = this.CourseTable.Clone();                                         
            foreach (DataRow row in searchResultRows)                                                       
            {
                searchResultTable.ImportRow(row);                                                           
            }
            this.dgv_Score.DataSource = searchResultTable;    
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(comboBox1.Text=="按编号查询"){
                SqlConnection sqlConnection = new SqlConnection();                                              
                sqlConnection.ConnectionString =
                    "Server=(local);Database=EduBase2018;Integrated Security=sspi";                             
                SqlCommand sqlCommand = new SqlCommand();                                                       
                sqlCommand.Connection = sqlConnection;                                                          
                sqlCommand.CommandText = "SELECT * FROM Shoufeixiangmu;";
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                           
                sqlDataAdapter.SelectCommand = sqlCommand;                                                      
                sqlDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;                          
                this.CourseTable = new DataTable();                                                             
                sqlConnection.Open();                                                                           
                sqlDataAdapter.Fill(this.CourseTable);                                                          

                sqlConnection.Close();    
            DataRow searchResultRow = this.CourseTable.Rows.Find(this.textBox9.Text.Trim());       
            DataTable searchResultTable = this.CourseTable.Clone();                                       
            searchResultTable.ImportRow(searchResultRow);                                                
            this.dgv_Score.DataSource = searchResultTable;  }
            else if (comboBox1.Text == "按名称查询")
            {
                SqlConnection sqlConnection = new SqlConnection();                                              
                sqlConnection.ConnectionString =
                    "Server=(local);Database=EduBase2018;Integrated Security=sspi";                            
                SqlCommand sqlCommand = new SqlCommand();                                                       
                sqlCommand.Connection = sqlConnection;                                                          
                sqlCommand.CommandText = "SELECT * FROM Shoufeixiangmu;";
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                          
                sqlDataAdapter.SelectCommand = sqlCommand;                                                   
                sqlDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;                            
                this.CourseTable = new DataTable();                                                           
                sqlConnection.Open();                                                                           
                sqlDataAdapter.Fill(this.CourseTable);                                                          

                sqlConnection.Close();
                                                               
                this.CourseViewByName = new DataView();                                                   
                this.CourseViewByName.Table = this.CourseTable;                                                 
                this.CourseViewByName.Sort = "名称 ASC"; 
                DataRowView[] searchResultRowViews =
                this.CourseViewByName.FindRows(this.textBox9.Text.Trim());                       
                DataTable searchResultTable = this.CourseTable.Clone();                                       
                foreach (DataRowView dataRowView1 in searchResultRowViews)                                      
                {
                    searchResultTable.ImportRow(dataRowView1.Row);                                              
                }
                this.dgv_Score.DataSource = searchResultTable;
            }
        }
    }
}

 

posted @ 2018-10-31 16:09  浮生。。  阅读(280)  评论(0编辑  收藏  举报