C# development-save query results to DataSet or DataTable (15.8)

I. Overview

  • The data can also be saved to the DataSet when performing a query on the data in the table, but it needs to be implemented with the help of the DataAdapter class.

  • In practical applications, DataAdapter and DataSet are the most used classes in query operations.

  • In addition, you can also add, modify, and delete data in the table through the DataSet

<!--more-->

Two Introduction to DataAdapter and DataSet Class

The DataAdapter class is used to query the data in the data table and add it to the DataSet. The corresponding class name of the DataAdapter in the System.Data.SqlClient namespace is SqlDataAdapter

2.1 The main construction method of the SqlDataAdapter class

Construction method Description
SqlDataAdapter(SqlCommand cmd) The constructor with parameters, passing the object of the SqlCommand class as a parameter
SqlDataAdapter(string sql, SqlConnection conn) The construction method with parameters, the sql parameter is to specify the SQL statement executed on the data table, and conn is the connection object of the database
SqlDataAdapter() Construction method without parameters

As can be seen from the construction method of the SqlDataAdapter class, the SqlDataAdapter class needs to be used with the SqlCommand class and the SqlConnection class

2.2 Commonly used properties and methods of the SqlDataAdapter class

Attribute or method Description
SelectCommand Properties, set the query statement to be executed in the SqlDataAdapter
InsertCommand Properties, set the add statement to be executed in the SqlDataAdapter
UpdateCommand Property, set the modification statement to be executed in SqlDataAdapter
DeleteCommand Properties, set the delete statement to be executed in the SqlDataAdapter
Fill(DataSet ds) Method to fill the DataSet object with the results of the query in the SqlDataAdapter class
Fill(DataTable dt) Method, the result of the query in the SqlDataAdapter class is filled into the DataTable object. The DataTable is a data table object, which is composed of multiple DataTable objects in a DataSet object
Update(DataSet ds) Method to update the data in the DataSet object
Update(DataTable dt) Method to update the data in the DataTable object

The DataSet class is a data set similar to the database structure. Each DataSet is composed of several data tables. DataTable is a data table. Each DataTable is also composed of rows and columns. Rows are represented by the DataRow class. The columns are represented by the DataColumn class.

In addition, users can also set the relationship between data tables through the DataRelation class

Three DataSet class and DataTable class

3.1 DataSet class

3.1.1 The constructor in the DataSet class

Construction method Description
DataSet() No-parameter construction method
DataSet(string DataSetName) Constructor with parameters, the DataSetName parameter is used to specify the name of the data set

3.1.2 Commonly used attributes and methods in the DataSet class

Attribute or method Description
Tables Property, get the collection of all data tables in the DataSet, Tables[0] represents the first data table in the collection
CaseSensitive Property, get or set whether the string in the DataSet is case sensitive
Relations Property to get the relational collection contained in the DataSet
Clear() Method to clear the data in the DataSet
Copy() Method to copy the data in the DataSet
AcceptChanges() Method to update the data in the DataSet
HasChanges () Method to get whether any data in the DataSet has changed
RejectChanges() Method to undo the changes to the data in the DataSet

3.2 DataTable

As an important object in the DataSet, DataTable is similar to the definition of the data table, which is composed of rows and columns, and has a unique table name

From the fill method (Fill) of the SqlDataAdapter class, it can be seen that data is allowed to be directly filled into the DataTable, which can save storage space and simplify the search for data in the data table.

3.2.1 Commonly used construction methods in DataTable

Construction method Description
DataTable() No-parameter construction method
DataTable(string TableName) The construction method with parameters, the TableName parameter is used to specify the name of the data table

3.2.2 DataTable and DataSet have many similar properties and methods

Attributes Description
TableName Property to get or set the name of the DataTable
Columns Property to get the collection of columns in the DataTable
Rows Property to get the collection of rows in the DataTable
DataSet Property to get the DataSet where the DataTable is located
Constraints Property to get all constraints in the DataTable

3.3 Use the DataSet and DataTable classes to store query results

In practical applications, you can store the query results in the DataSet class or the DataTable class, and it is very similar when you manipulate the query results.

The following are examples to demonstrate the use of DataSet and DataTable.

Four examples Create a Windows application, query all user names in the user information table (userinfo), and display the user names in the list control (ListBox)

4.1 Analysis

Click the "Query all user names" button to display all user names in the ListBox control, first use the DataSet object to store the query results

4.2 Interface layout

Contains the following components:

  • Query all user names: Button

  • Data display: ListBox

     

 

4.3 Code

//The click event of the "query all users" button
private void button1_Click(object sender, EventArgs e)
{
    //Write database connection string
    string connStr = "Data Source=.; Initial Catalog=test;User ID=sa;Password=root";
    //创建 SQLConnection 的实例
    SqlConnection conn = null;
    try
    {
        conn = new SqlConnection(connStr);
        //打开数据库连接
        conn.Open();
        string sql = "select name from userinfo";
        //创建 SQLDataAdapter 类的对象
        SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
        //创建DataSet类的对象
        DataSet ds = new DataSet();
        //使用SQLDataAdapter对象sda将查询结果填充到Dataset对象ds中
        sda.Fill(ds);
        //设置ListBox控件的数据源(DataSource)属性
        listBox1.DataSource = ds.Tables[0];
        //在listBox控件中显示name列的值
        listBox1.DisplayMember = ds.Tables[0].Columns[0].ToString();
    }
    catch(Exception ex)
    {
        MessageBox.Show("查询失败!" + ex.Message);
    }
    finally
    {
        if (conn != null)
        {
            //关闭数据库连接
            conn.Close();
        }
    }
}

4.4 效果图

 

4.5 说明

从上面的运行效果可以看出,已经将用户信息表 (userinfo) 中的所有用户名显示在列表控件 (ListBox) 中。

需要注意的是,ListBox 控件中的 DataSource 属性用于设置控件中内容的数据源,并需要通过 DisplayMember 属性来指定显示在 ListBox 控件中的内容

4.6 延伸 在本实例中将 DataSet 对象换成 DataTable 对象

//创建 SqlDataAdapter 类的对象
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
//创建 DataTable 类的对象
DataTable dt = new DataTable();
//使用 SqlDataAdapter 对象 sda 将查询结果填充到 DataSet 对象 dt 中
sda.Fill(dt);
//设置 ListBox 控件的数据源(DataSource)属性
listBox1.DataSource = dt;
//在 ListBox 控件中显示 name 列的值
listBox1.DisplayMember = dt.Columns[0].ToStiring();

 

Guess you like

Origin blog.csdn.net/Calvin_zhou/article/details/108066298
Recommended