The previous article introduced how to use LINQ to manipulate weak dataset,
This blog post describes how to use LINQ to manipulate a strongly Typed Dataset,
What is a strong dataset?
In fact, a dataset file (such as mydataset. XSD) is used to produce a dataset,
In this case, dataset is strongly typed,
To understand a strongly typed dataset,
First, you must know what the dataset file is like,
This is a complete dataset file that allows you to manipulate data tables using designer,
Therefore, you can drag and drop data tables in several databases from the server resource manager to this. XSD dataset,
In this way, you can get
In the figure above, we can see that I dragged two data tables into the dataset file,
In the process of dragging and dropping data tablesCode,
You can see that there is a class demo7use under the namespace csharplanguage,
Then there are three classes under csharplanguage. demo7usetableadapters,
One is "studenttableadapter" and the other is "author tableadapter"
Another is tableadaptermanager"
The designer creates a class for each data table you drag,
This class is centrally managed by csharplanguage. demo7usetableadapters,
Next, let's look at an example of using the demo7use. XSD file.
Using system;
Namespace csharplanguage
{
Public partial class demo _ 7: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
// Use the Dataset Designer Wizard to reference a data table,
// Actually, only a class is completed,
// This class is completed by.
// While the dataset class name is the file name when the dataset file is created
// If you want to use the content in this dataset,
// Like other classes, you must instantiate them first.
// A dataset must be instantiated first.
Demo7use DS = new demo7use ();
// The demo7usetableadapters implements the sqldataadapter
// View demo7use. Designer. CS.
// Vs automatically generates a demo7usetableadapters class,
// In fact, this class name is composed of your dataset file name and tableadapters.
// That is to say, vs automatically stores every table in your dataset File
// A corresponding tableadapter is generated under the dataset file name tableadapters.
// The combination of the data table name and tableadapter
// For example, I created a demo7use. XSD dataset file,
// The Name Of The tableadapter set will be demo7usetableadapters.
// In This dataset file, I dragged another student User table.
// In the tableadapter set demo7usetableadapters, another
// Studenttableadapter
// In fact, you can regard this studenttableadapter as a sqldataadapter
// This studenttableadapter only belongs to the student table.
// Sqldataadapter, while sqldataadapter can operate on any table
Demo7usetableadapters. studenttableadapter Studentadapter =
New csharplanguage. demo7usetableadapters. studenttableadapter ();
// Fill the data table with the sqldataadapter -- adapter that only works for the student table
Studentadapter. Fill (Ds. Student );
Gridview1.datasource =DS. student;
Gridview1.databind ();
// Then I drag a data table "author" into the dataset file demo7use. XSD"
// This will generate a "author tableadapter" in the "demo7usetableadapters" set"
// You can use the sqldataadapter that only works for the "author" table.
// Fill in the "author" table in the DS Dataset
Demo7usetableadapters. Author: tableadapter Authoradapter =
New csharplanguage. demo7usetableadapters. Author tableadapter ();
Authoradapter. Fill (Ds. Author );
Gridview2.datasource = DS. Author ;
Gridview2.databind ();
}
}
}
Let's take a look at the demo execution result above.
The demo above has written a very detailed annotation. If you are not clear about it, you must pay attention to the annotation.
This section describes how to use a dataset file,
The following code uses LINQ to dataset to operate the dataset,
Please note that,
When using LINQ to dataset, if the dataset type is weak,
Make sure that the datatable in dataset,
To implement the returned object of the ienumerable or ienumerable <t> interface,
If dataset is of a strong type, you do not need to consider this for a long time,
Because
From the two above, we can see that if dataset is of a strong type,
Vs automatically creates a separate class for each table dragged into the dataset file,
And this class inherits from system. Data. typedtablebase,
The system. Data. typedtablebase class implements the ienumerable <t> interface,
Therefore, the data table object in the dataset file indirectly implements the ienumerable <t> interface,
Therefore, when using a strongly typed dataset, you do not need to consider these two interfaces.
For the use of a strong data set of the LINQ to type, see the following example,
In fact, in addition to the code-behind section,
The others are exactly the same, because it is the demo completed by adding LINQ to the above example,
Using system;
Using system. Data;
Namespace csharplanguage
{
Public partial class demo _ 7: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Demo7use DS = new demo7use ();
Demo7usetableadapters. studenttableadapter studentadapter =
New csharplanguage. demo7usetableadapters. studenttableadapter ();
Studentadapter. Fill (Ds. Student );
Gridview1.datasource = Ds. student;
Gridview1.databind ();
Demo7usetableadapters. Author: tableadapter authoradapter =
New csharplanguage. demo7usetableadapters. Author tableadapter ();
Authoradapter. Fill (Ds. Author );
Gridview2.datasource = Ds. Author;
Gridview2.databind ();
VaR studentresult = from P in DS. Student
Select New
{
P. ID card number,
P. Student name,
P. Gender
};
VaR authorresult = from P in DS. Author
Select New
{
P. Author code,
P. Author's name,
P. Gender
};
Gridview3.datasource = studentresult;
Gridview3.databind ();
Gridview4.datasource = authorresult;
Gridview4.databind ();
}
}
}
Then let's take a look at the execution results.
The above is a complete example,
Some annotations should be carefully considered.
2010-2-01