Dataset Overview
1.1 Dataset
L is a memory resident structure that represents Relational Data
L is a data view in XML format. It is a data relationship view.
L in Visual Studio and. NET Framework, XML is the format used to store and transmit various data. Therefore, datasets are closely related to XML.
1.2 dataset Classification
-Typed Dataset
-Untyped Dataset
1.3 differences between a typed dataset and a non-Typed Dataset
Architecture
Function Type
Detection
Typed Dataset
A typed dataset is a type of dataset that first derives from the base dataset class and then generates a new class using the information in the XML schema file (. XSD file. Information in the architecture (tables, columns, etc.) is generated and compiled as the First Class Object and attribute of a group for this new dataset class.
You can directly use the name
Reference Tables and columns
In vs. net, you can intelligently perceive the types of elements.
Untyped data
Set
Untyped datasets do not have a built-in architecture. Like typed datasets, untyped datasets include tables, columns, and so on, but they are only published as collections.
You need to use the tables set
Reference Columns
Imperceptible
1.4 dataset attributes
Important:
-Tables
-Relations
Create a dataset and a data table
2.1 create a dataset:
-Create an untyped dataset during design
Toolbox "data"
-Create an untyped dataset at run time
2.2 create a data table
• Data table attributes
-The most important collection:
• Columns
• Rows
• Constraints
• Add a data table to a dataset
[ReferenceCode]
// Create a dataset
Dataset DS = new dataset ("myds ");
Datatable dsmaster = new datatable ("master ");
Datatable dschild = new datatable ("child ");
DS. Tables. Add (dsmaster );
DS. Tables. Add (dschild );
2.3 add columns to a data table
• Columns set attributes
• Add columns to a data table
2.4 Add rows to a data table
• Rows attributes
• Rows. Add Method
-Add (datarow): Add a specified data row to the data table.
• Add rows to a data table
[Reference Code]
// Add a column
DS. Tables ["master"]. Columns. Add ("masterid", typeof (int32 ));
// Add rows
Datarow DR = Ds. Tables ["master"]. newrow ();
Dr ["masterid"] = "";
DS. Tables ["master"]. Rows. Add (DR );
// Modify the header
DS. Tables ["master"]. Columns ["masterid"]. Caption = "Master ID ";
2.5 add constraints for Data Tables
• Foreignkeyconstraints attribute: ensures the relationship between two data tables when the corresponding row changes
• Uniqueconstraints attribute: Ensure that data in a column is different in each row
• Add constraints to data tables during runtime
-Add a foreign key constraint: Modify the value of a table to see if it changes
-Add a unique key constraint
2.6 Add a link to a dataset
• Relations: defines the relationship between data tables. This set can contain 0 or more data relationship objects, each representing
Relationship between two tables
• The constraints created in datarelation are enforced only when the enforeconstraints attribute of the dataset is set to true.
• Add data relationships to data tables (non-type datasets)
[Reference Code]
// Add a unique key
System. Data. uniqueconstraint UC = new uniqueconstraint ("unique key name", DS. Tables ["master"]. Columns ["masterid"]);
// Add a foreign key
System. Data. foreignkeyconstraint fc = new foreignkeyconstraint ("foreign key name", "main table column", "Slave table column ");
// Add Link
DS. relations. Add ("Link name", "parent table column", "Child table column ");
// Note the difference between the link and the foreign key
If (Ds. relations. Count <= 0)
Return;
System. Data. datarow DR = Ds. Tables ["master"]. Rows [0];
System. Data. datarow [] drarray = dr. getchildrows (Ds. Relations [0]);
// The difference here is that the foreign key: if the primary table column is updated, the slave table column is updated accordingly.
// Link: obtains the row set corresponding to the sub-table from a column corresponding to the parent table.
2.7 display row status
• The rowstate attribute of the Data row reflects the operations performed on the data table since the data table was created or after the last update.
• Datarowstate Value
• Display status lines
[Reference Code]
// Display the row status
Datarowstate DRS = Ds. Tables ["master"]. Rows [0]. rowstate;
Data Operations
3.1 dataset Method
-Clone a dataset: only copy the structure
Dataset. Clone ();
-Copy a dataset: copy the structure and data.
Dataset. Copy ();
3.2 data table method
-Select method: used to filter and sort data table rows at run time. It does not change the table content. This method only returns the array of rows that match the specified rule.
// Return the datarow [] Set
Dataset. Tables ["master"]. Select ("condition", "sorting rule ");
Filtering and sorting in a dataset
After filling in a dataset, we usually find it useful to use different record subsets in a table or view data in different order. You can filter and sort data in a dataset. To simplify this process, you can create a data view that provides objects that can combine filters and sorting conditions for data binding.
Filtering and sorting in a dataset
You can use the built-in dataset function to filter and sort data. There are two options:
• Data Tables support the select method. You can use this method to filter and sort data tables. This method does not change the record content and sequence in the table. Instead, it provides you with a record list (OR array) to indicate the specified conditions.
• Data View (dataview object) can be used ). A data view is an object that acts as a layer on top of a data table and provides a filtered and sorted table content view. (You can also use the data view Manager, which acts like a data view set .) A data view is similar to a database view because it is not a copy of data. On the contrary, it is just another way to view data in a table.
4.1 Data View Overview
• Data view is an independent object located on top of a data table
• The data view is the view after filtering and sorting a single data
• Can be used as a data source to bind controls
• Multiple Data views can be created for a data table
• The view data row actually references the datarowview object of the Data row.
Attributes of datarowview
Attribute
Description
Dataview
The data view to which the data row view belongs.
Isedit
Indicates whether the data row view is being edited.
Isnew
Indicates whether the data row view is new.
Item
Value of a column in the Data row view
Row
Data row being viewed
Rowversion
Current version of the data row view
4.2 create a data view
• Created at design time
-Create a Typed Dataset
-Select the "dataview" control from "data" in the toolbox.
• You can use the defaultview attribute of the table to access the default data view.
Dataview object. You can set the default data view attribute at runtime.
4.3 Data view attributes
• Data view attributes
-Rowfilter
-Rowstatefilter
-Sort
• Use the select method of the table or the rowfilter attribute of the Data View to filter records in the data table so that only the records you want to operate can be available. This is useful when you want to operate on different subsets recorded in the dataset table. To specify filter conditions, you can use the same expression syntax as the syntax used to create column expressions.
[Reference Code]
// Rowfilter judgment
String tempid = This. lbdid. Text. tostring ();
Bookview. rowfilter = "id = '" + tempid + "'";
If (bookview. Count = 1)
{
Response. Write ("<SCRIPT langusge = 'javascript '> alert ('... '); </SCRIPT> ");
Return;
}
// Rowstatefilter judgment
Dataview dwmain = (dataview) session ["cart"];
Dwmain. rowfilter = This. tbcon. text;
Switch (ddlstate. selectitem. value)
{
Case "currentrows ":
Dwmain. rowfilter = dataviewrowstate. currentrows;
Break;
Case "originalrows ":
Dwmain. rowfilter = dataviewrowstate. originalrows;
Break;
Case "added ":
Dwmain. rowfilter = dataviewrowstate. added;
Break;
Case "unchanged ":
Dwmain. rowfilter = dataviewrowstate. unchanged;
Break;
Case "modifiedcurrent ":
Dwmain. rowfilter = dataviewrowstate. modifiedcurrent;
Break;
Default:
Break;
}
Data column expression
• A string that can be generated using any common string Handler
• Arithmetic Operators: +,-, *,/, %
• Comparison operators: And, Or, <, >,< =, >=, <>
-In: determines whether the specified value is included in a collection.
• Myexpression = "mycolumnin ('A', 'B', 'C ')"
-Like: Fuzzy match with wildcards
• Functions
4.4 Data View Method
Method
Description
Addnew
Add a new data row view to the Data View
Delete
Delete A data row view from the Data View
Find
Queries the data row view containing the primary key based on the specified Primary Key.
Find Method
• Search for rows in dataview with the specified sorting keyword value.
[Reference Code]
// Add rows to the Data View
Datarowview DRV;
DRV = dwmain. addnew ();
DRV ["..."] = "..";
// Omitted, no need to add
// Delete
Dwmain. Delete (dwmain. Count-1 );
// Update
Dwmain [dwmain. Count-1] ["column name"] = "";
// Search, returns the number of retrieved rows
Dwmain. Find ("keyword ");
Practical dataset skills
• Excel and Dataset
[Reference Code]
// Add the namespace using system. Data. oledb;
# Region import dataset from Excel
String strconn = "provider = Microsoft. Jet. oledb4.0" +
"Data Source =" + strfillname + ";" +
"Extended properties = excel8.0 ";
Oledbdataadapter excelda = new oledbdataadapter ("select * from [sheet1 $]", strnconn );
Dataset excelds = new dataset ();
Try
{
Excelda. Fill (excelds, "excelinfo ");
}
Catch (exception ERR)
{
// Handle errors
}
# Endregion
# Region import EXCEL from Dataset
// Add COM component first: Add reference-COM component-Microsoft Excel 11.0-select confirm
Excel. Application Excel = new excel. Application ();
Int rowindex = 1;
Int colindex = 0;
Excel. application. workbooks. Add (true );
System. Data. datatable TB = Ds. Tables [0];
Foreach (datacolumn Col in TB. columns)
{
Colindex ++;
// Obtain the column name
Excel. cells [1, colindex] = col. columnname;
}
Foreach (datarow row in TB. Rows)
{
// Add data
Rowindex ++;
Colindex = 0;
Foreach (datacolumn Col in TB. columns)
{
Colindex ++;
Excel. cells [rowindex, colindex] = row [col. columnname]. tostring ();
}
}
Excel. Visible = false;
Excel. activeworkbook. saveas (....);
Excel. Quit ();
Excel = NULL;
GC. Collect (); // garbage collection
# Endregion
• XML and Dataset
[Reference Code]
Dataset DS = new dataset ();
// Schelma should be loaded separately as much as possible; otherwise, the resolution efficiency is very low.
DS. readxml ("...");
DS. writexml ("..");
• Blob and Dataset