A simple introduction to Ado.net Typed DataSet _ Practical Tips

Source: Internet
Author: User
Tags connectionstrings

Disadvantages of a weak type dataset:
1, can only be referenced by the column name, DataSet. Tables[0]. Rows[0]["Age", if you write the column name incorrectly, you will not find an error compiling, so you must remember the column name when you develop it.
2, int age=convert.toint32 (dataset). Rows[0]["Age"], the value of the field is object type, you must be careful to type conversion, not only troublesome, but also error prone.
3. The dataset is passed to other users, and it is difficult for the user to identify which columns are available for use.
4, run to know all column names, data binding trouble, can not use WinForm, ASP. NET's fast development function.
5. Write your own strongly typed dataset (typed Dataset,typeddataset), create a Persondataset class that inherits from the DataSet, and encapsulate an int? Age and other methods, such as bool isagenull, to fill the persondataset.

Ii. vs automatic generation of strongly typed datasets:
1. Step: Add-> New Item-> DataSet
2. Drag and drop the table from Server Explorer into the dataset. Note that the drag-and-drop process is automatically generated by the table structure of a strongly typed DataSet and other classes, not the data also dragged over, the program is still linked to the database, the database connection string is automatically written in the app.config.
3, using the dataset example in your code: Cc_recordtableadapter adapter=new cc_recordtableadapter (); How do I know adapter's class name? The Adapter,name property of the lower half of the dataset is selected as the class name. Need to right-click class name-> parsing
4, get all the data: adapter. GetData (), example program: Traversal display all data, I<adapter. GetData (). Count;adapter. GetData () [i]. Age.
5, frequently asked Questions: The class name knocks incorrectly, the table name +tableadapter, the table name +datatable, the table name +row, and then uses "resolves" to populate the class name.
6. Frequently Asked Questions: class-defined classes are referenced by the full name of the namespace, and cannot be omitted. Class can avoid a problem where the class cannot duplicate the same namespace.

Iii. Update DataSet:
1, call the adapter Update method to save the dataset changes to the database. Adapter. Update (DataTable);
2. To invoke Update method updates, you must set the database primary key, as well as the Delete method;
3. Common error: "When passing a DataRow collection with modified rows, update requires a valid UpdateCommand" to set the primary key for the table. "Who has changed, only the primary key will not change", the program to use the primary key to locate the row to be updated. Forgot to set the primary key to do? First set the primary key in the database, then right-click on the dataset's corresponding DataTable, select "Configure" and click "Finish" in the dialog box. Good habit: All tables have to set PRIMARY KEY!!! See why it will automatically help us GetData, Update, Delete.
Now do a simple exercise:
First step: Add a database named Db1.mdf (table T_persons contains id,name,age fields)
Step Two: Add an application configuration file: App.config file with the following code:

Copy Code code 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 three: Add another DataSet file: Datasetpersons.xsd, and drag the table t_persons onto the dataset.
Step Fourth: Put a button on the form Form1 interface, displaying all the name in the database table one at a time when you click it. The form code is as follows:

Copy Code code 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 typed 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, then fill class name with ' parse '
T_personstableadapter adapter = new T_personstableadapter ();
Typed DataSet.DataSetPersons.T_PersonsDataTable personstable = adapter. GetData ();
for (int i = 0; i < personstable.count i++)//If PersonsTable.Rows.Count is a weak type
{
Type DataSet.DataSetPersons.T_PersonsRow person = personstable[i];
MessageBox.Show (person. Name);
}
}
}
}

Reminder: For the above reference class internal class, the way to write the class is: Table name +tableadapter, table name +datatable, table name +rows, and then use "Resolution" to populate the class name.

Iv. Other issues:
1, insert a new row, invoke the Insert method.
2. What to do when adding a field in the database table? In the DataSet Designer, click Configure, and in the dialog box, click Query Builder and tick the newly added fields. Deleting a field is also true.
3, to modify the field to reconfigure the build, which is a strong type dataset weakness.
4, common errors: Error, data is empty. method to determine the null value of a column: Is**null
5. Why is the Select method populated, the Update method is updated, and the Insert method is inserted?  Look at the adapter of the SelectCommand and other attributes are known, are those SQL statements at work, if necessary can be fully adjusted manually. Such as:

Copy Code code as follows:

Personstable[0]. Name = "Lucy";
Adapter. Update (personstable)//Call the Update method to update the dataset's modifications to the database
Adapter. Insert ("John", 50);

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.