Introduction to DataSet objects in ASP.net and asp. netdataset
When we perform operations on the database, we always extract the data from the database and put it in a "Container". Then, we retrieve the data from the "container" and display it at the front-end, dataSet is the most common and important role in the container. It can be said that DataSet serves as a bridge between the database and the front-end.
The following describes the usage of DataSet. (DataSet object itself does not have the ability to access the database. It must be used in combination with DataAdapter. However, the usage of DataAdapter is described in another article, you can take a look)
DataSet can contain multiple data tables and dynamically generate data tables in the program. The data tables here can come from databases, files, or XML (the XML usage has been mentioned earlier, if you don't know much about it, you can check it out ). DataSet allows you to browse, edit, sort, filter, and create views of data.
DataSet uses a non-connection transmission mode to access the database. That is, after the data requested by the user is read into the DataSet, the connection to the database is closed, so that other users can continue to use the data source.
Since I used DataSet for so long, I have not only used fixed statements about DataSet, but realized that the structure of DataSet is the most critical. The structure of the DataSet object is as follows:
DataSet object attributes and methods:
DataSetName: Get or set the name of the DataSet object
Tables
Obtains the data table collection of a DataSet. All able objects of the DataSet object belong to the DataTableCollection.
The most common attribute is Tables. Through this attribute, you can obtain or set the values of data table rows and columns.
For example, the expression "DS. Tables [" ShoppingCart "]. rows (I). Item (j)" indicates column j of row I of the ShoppingCart table.
The most common methods are Clear () and Copy (). Clear, which are used to Clear data of the DataSet object and delete all DataTable objects. Copy method copies the structure and data of the DataSet object. The returned value is a DataSet object with the same structure and data as the current DataSet object.
Simple Example: (there is no front-end code, and only some data related to DataSet is in the background. However, I will upload this example later. If you are interested, you can check it out)
[Csharp]View plaincopyprint?
- Protected void Page_Load (object sender, EventArgs e)
- {
- Sqlconnection of the connected database is temporarily omitted
- SqlDataAdapter sqglad = new SqlDataAdapter (SQL, con); // create a SqlDataAdapter object and execute an SQL query
- DataSet ds = new DataSet (); // create a DataSet
- Sqglad. Fill (ds); // Fill the dataset
- GridView1.DataSource = ds. Tables [0]. DefaultView; // set the data source
- GridView1.DataBind (); // bind a data source
- }
Functions and usage of aspnet dataset
Dataset is an important core object of ADO. NET. It is used to store temporary data tables and use XML at the underlying layer of the object.
Storage in memory is resource-consuming.
For example
// Data adapter.
SqlDataAdapter da = new SqlDataAdapter (sqlStr, con );
// DataSet object.
DataSet ds = new DataSet ();
// Fill in the dataset.
Da. Fill (ds, tableName );
// After filling, you can use the table in the dataset.
Dv = ds. Tables [tableName]. DefaultView;
How to receive the DataSet object (ASPNET) returned by a custom function)
For example, the most common Layer 3
UI BM DA
Access the method in the namespace of the UI using BM layer.
Access the methods in the namespace of the BM using DA layer.
Dataset ds = dataset returned by the function
Ds is the returned object! You can assign ds to the required control! You can also control. datasouser = dataset returned by the function.