A brief introduction to the ADO type DataSet

  • 2020-06-03 06:17:41
  • OfStack

1. Disadvantages of weak DataSet:
1, only by column name reference, dataset. Tables[0].Rows[0]["Age"], if the wrong column name will not be found when compiling, so development must remember the column name.
2, int age= Convert.ToInt32 (dataset.Rows [0]["Age"]), get the field value is object type, must be carefully cast, not only troublesome, but also prone to error.
3. Passing DataSet to other users makes it difficult for users to identify which columns are available for use.
4, run time to know all the column names, data binding trouble, can not use Winform, ES20en. Net rapid development function.
5. Write strong DataSet(typed DataSet, TypedDataSet) by yourself, create PersonDataSet class inherited from DataSet, and encapsulate int? Attributes such as Age and methods such as bool IsAgeNull are populated into PersonDataSet.

2. VS automatically generates strong type DataSet:
1. Step: Add - > New items - > The data set
Drag and drop the table from server Explorer into DataSet. Note that the drag and drop process automatically generates classes such as DataSet based on the table structure. Instead of dragging the data over, the program still connects to the same database and automatically writes the database connection string in ES43en.Config.
3. DataSet example: CC_RecordTableAdapter adapter=new CC_RecordTableAdapter(); How do I know the class name of Adapter? Select Adapter in the bottom half of DataSet. The Name attribute is the class name. You need to right click on the class name - > parsing
4, get all the data: ES59en. GetData(), example procedure: traversal display all the data, i < adapter. GetData (). Count; adapter. GetData () [r]. i Age.
5, FREQUENTLY asked questions: type wrong class name, table name +TableAdapter, table name +DataTable, table name +Row, and then use "parse" to fill in the class name.
6, FREQUENTLY asked questions: Classes defined internally are referenced by including namespace's full name and cannot be omitted. Classes defined internally avoid the problem of having the same name as a class under namespace.

3. Update DataSet:
1. Call the Update method of Adapter to save the changes of DataSet to the database. adapter. Update (datatable);
2. To call the Update method for update, the primary key of the database must be set, and the same is true for the Delete method;
3. Common error: "When passing the DataRow collection with modified rows, the update requires a valid UpdateCommand" to set the primary key for the table. "Everything changes except the primary key," which the program USES to locate the row to be updated. What if I forgot to set the primary key? Go to the database and set the primary key, then right click on the corresponding DataTable for DataSet, select Configure, and click Finish in the dialog box. Good habit: all tables should have primary keys!! See why it automatically helps us GetData, Update, Delete.
Now do a simple exercise:
Step 1: Add a database named DB1.mdf (Table T_Persons contains fields Id,Name,Age)
Step 2: Add an application profile: App. config file with the following code:

<?xml version="1.0" encoding="utf-8" ?>
        <add name=" The typed DataSet.Properties.Settings.DB1ConnectionString"
            connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DB1.mdf;Integrated Security=True;User Instance=True"
            providerName="System.Data.SqlClient" />

Step 3: Add another dataset file: ES114en.xsd and drag the table T_Persons onto the dataset.
Step 4: Place the 1 button on the Form Form1 interface and, when clicked, display all Name in the database table one by one. The form 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  The typed DataSet.DataSetPersonsTableAdapters;

 namespace  The typed DataSet
     public partial class Form1 : Form
         public Form1()

         private void Show_Click(object sender, EventArgs e)
             // The name of the table +TableAdapter And the name of the table +DataTable And the name of the table +Rows , and then fill in the class name with parse 
             T_PersonsTableAdapter adapter = new T_PersonsTableAdapter();
              The typed DataSet.DataSetPersons.T_PersonsDataTable personsTable = adapter.GetData();
             for (int i = 0; i < personsTable.Count; i++)// If be, personsTable.Rows.Count It becomes a weak type 
                  The typed DataSet.DataSetPersons.T_PersonsRow person = personsTable[i];

Note: In the case of referring to a class inside the class above, the method for writing the class is: table name +TableAdapter, table name +DataTable, table name +Rows, and then fill in the class name with "parse".

4. Other questions:
1. Insert new row and call Insert method.
2. What should I do after adding fields to the database table? Click [Configuration] in DataSet designer, click [Query Builder] in the dialog box, and check the newly added fields. The same goes for deleting fields.
3. To modify fields, you need to reconfigure the generation, which is the weakness of the strong type DataSet.
4. Common errors: error and null data. Is**Null to determine if the value of a column is null
5. Why is Select filled, Update updated, and Insert inserted? Take a look at Adapter's SelectCommand and other attributes. These are all SQL statements that work, and can be adjusted manually if necessary. Such as:

     personsTable[0].Name = "Lucy";
            adapter.Update(personsTable);// call Update Method updates changes to the dataset to the database 
            adapter.Insert("John", 50);

Related articles: