A Brief Introduction to ADO. Net Typed DataSet

Source: Internet
Author: User

I. disadvantages of weak DataSet:
1. It can only be referenced by column names, dataset. tables [0]. rows [0] ["Age"]. If the column name is incorrectly written and compiled, no error is found. Therefore, you must remember the column name during development.
2. int age = Convert. toInt32 (dataset. rows [0] ["Age"]), the value of the obtained field is of the object type, and type conversion must be carefully performed, which is not only troublesome, but also prone to errors.
3. Passing DataSet to other users makes it difficult for users to identify which columns are available for use.
4. You can only know all the column names at runtime. It is difficult to bind data and you cannot use the quick development functions of Winform and ASP. Net.
5. Write a strong DataSet (typed DataSet and TypedDataSet) by yourself, create a PersonDataSet class inherited from DataSet, and encapsulate int? Age and other attributes and bool IsAgeNull and other methods, fill in the PersonDataSet.

Ii. VS automatically generates a strong DataSet:
1. Step: Add> new item> Dataset
2. Drag and Drop a table from the server resource manager to DataSet. Note that the drag-and-drop process automatically generates strong DataSet and other classes based on the table structure, without dragging the data, the program is still connected to the database, and the database connection string is automatically written in App. Config.
3. sample DataSet used in the Code: CC_RecordTableAdapter adapter = new CC_RecordTableAdapter (); how do I know the class name of the Adapter? Select the Adapter in the lower half of the DataSet. The Name attribute is the class Name. You need to right-click the class name-> resolve
4. Get all the data: adapter. getData (), example program: traverse all data, I <adapter. getData (). count; adapter. getData () [I]. age.
5. FAQ: the class name is incorrect. The table name + TableAdapter, the table name + DataTable, the table name + Row, and the class name is filled with "resolution.
6. FAQ: the internal definition of a class must be referenced by the full name of the namespace. The class defined inside the class can avoid the problem that the class cannot be renamed in the same namespace.

3. Update DataSet:
1. Call the Update method of the Adapter to save the DataSet changes to the database. Adapter. Update (datatable );
2. To call the Update method, you must set the primary key of the database. Likewise, the Delete method is the same;
3. common error: "When a DataRow set with modified rows is passed, the update requires a valid UpdateCommand." Set a primary key for the table. "Everyone has changed. Only the primary key will not change." The program uses the primary key to locate the row to be updated. What should I do if I forget to set the primary key? First, set the primary key in the database, right-click the corresponding able of the DataSet, select "configuration", and click "finish" in the dialog box ". Good Habit: Set a primary key for all tables !!! Let's see why GetData, Update, and Delete are automatically supported.
Now let's do a simple exercise:
Step 1: Add a database named DB1.mdf (Table T_Persons contains the Id, Name, Age field)
Step 2: add an application configuration file: App. config. The Code is as follows:

Copy codeThe Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Configuration>
<ConfigSections>
</ConfigSections>
<ConnectionStrings>
<Add name = "Typed DataSet. Properties. Settings. DB1ConnectionString"
ConnectionString = "Data Source =. \ SQLEXPRESS; AttachDbFilename = | DataDirectory | \ DB1.mdf; Integrated Security = True; User Instance = True"
ProviderName = "System. Data. SqlClient"/>
</ConnectionStrings>
</Configuration>

Step 3: add the dataset file DataSetPersons. xsd and drag the table T_Persons to the dataset.
Step 4: place a button on the form Form1 interface. When you click it, all names in the database table are displayed one by one. The form code is as follows:

Copy codeThe Code is as follows:
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 type DataSet. DataSetPersonsTableAdapters;

Namespace Typed DataSet
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}

Private void Show_Click (object sender, EventArgs e)
{
// Table name + TableAdapter, table name + DataTable, table name + Rows, and then fill the class name with "resolution"
T_PersonsTableAdapter adapter = new T_PersonsTableAdapter ();
Typed DataSet. DataSetPersons. T_PersonsDataTable personsTable = adapter. GetData ();
For (int I = 0; I <personsTable. Count; I ++) // if it is personsTable. Rows. Count, it becomes weak.
{
Typed DataSet. DataSetPersons. T_PersonsRow person = personsTable [I];
MessageBox. Show (person. Name );
}
}
}
}

Reminder: for the class referenced above, the method for writing the class is: Table name + TableAdapter, table name + DataTable, table name + Rows, then fill in the class name with "resolution.

4. Other problems:
1. Insert a new row and call the Insert method.
2. What if fields are added to the database table? In the DataSet Designer, click Configure. In the dialog box, click query generator. Select the newly added field. The same is true for deleting fields.
3. to modify a field, you must reconfigure and generate it. This is the weakness of the strong-type DataSet.
4. Common Errors: an error is reported and the data is empty. Method for determining whether the column value Is Null: Is ** Null
5. Why is the Select method filled, the Update method updated, and the Insert method inserted? Check the SelectCommand and other attributes of the Adapter. All these SQL statements work. You can adjust them manually if necessary. For example:
Copy codeThe Code is as follows:
PersonsTable [0]. Name = "Lucy ";
Adapter. Update (personsTable); // call the Update method to Update the changes to the dataset to the database.
Adapter. Insert ("John", 50 );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.