Use the dataset. XSD file of the Visual Studio Dataset

Source: Internet
Author: User

I recently developed the C/S architecture software. I just copied it when I read this article in many aspects that I was not familiar with. Thanks very much to the original author. The following is the original article.

 

In daily development, in order to write functions such as adding, updating, modifying, and deleting data, you have to deal with boring code and repeat the work .. The release of the official version of NET 2.0 is undoubtedly a great pleasure for our program developers. Some new enhancements of Visual Studio 2005 and ADO. the new features of NET 2.0 make it easier and faster to develop highly scalable multi-tier database applications.

In. NET 1.1 can use a strong data set, but it is inconvenient to develop it in Visual Studio 2003. In Visual Studio 2005, the design view of dataset is greatly improved, because ADO. NET 2.0 adds the tableadapter object, so you can also add the table adapter of the datatable in the dataset design view. In the past, typeddatasets was only a set of data entities (dummy objects, only attribute classes), and it was not convenient for self-management persistence and data integrity verification. Now we can easily implement these features.

The following example describes how to use vs2005 to generate a strong data set to simplify the development process and generate a scalable multi-layer database application.

First, create a new web site project. In [solution Resource Manager] → [add new project] → [dataset], name it authors. XSD, because the strong-type dataset needs to be placed in the app_code directory, if the project file does not have the app_code directory, the IDE will prompt whether to create it. We can select "yes.

At this time, the IDE will automatically open the authors we just created. XSD file, and start running the tableadapter Configuration Wizard, if the web. config does not have a database link string. In this case, we need to create a new connection: choose [new link] → [Microsoft SQL Server] → [select the built-in pubs database] → [Save the new connection string to the application configuration file].

In the next selection of the command type, IDE comes up with three options for the user to choose to access the database form:

1. Use SQL statements. For a single table, the wizard can automatically generate insert, update, and delete statements. This method is also used in this example.

2. Create a New stored procedure. For a single table, the wizard can automatically generate insert, update, and delete statements.

3. Use existing stored procedures. Select an existing stored procedure for each command.

Here we select [use SQL statement] → [query generator] → [authors table, select all columns]. At this time, the query generator will automatically generate an SQL statement (select authors. * From authors ). [Advanced options], you can choose whether to generate insert, update, and delete statements, use open concurrency to prevent concurrency conflicts, refresh the data table, and verify the insert and update statements, in order to give a clearer introduction to the use method, we will not select it here, but we will add it as needed. [Select the method to be generated], we only need to use [return datatable], and remove the check mark of other options.

The create Wizard will automatically generate "SELECT statement", "ing of data tables", and "Get method" for us ". The creation process is complete. This means that dataset. XSD will automatically generate an authors table and an authorstableadapter dataset containing the get method.

Similarly, right-click the created data table [authorstableadapter] → Add a query. Refer to the preceding method to generate deletequery (delete), insertquery (ADD), and updatequery (update), selectdetail (return detailed results, use the select (return row) option to generate a dataset that only contains one piece of data) and scalarquery (return data statistics ).

The following are generated SQL statements that need to be manually modified into the code you need:

Deletequery: delete from [authors] Where ([au_id] = @ original_au_id)

Insertquery: insert into authors (au_lname, au_fname, phone, address, city, state, zip, contract) values (@ au_lname, @ au_fname, @ phone, @ address, @ city, @ State, @ zip, @ Contract)

Updatequery: Update authors set au_lname = @ au_lname, au_fname = @ au_fname, phone = @ phone,
Address = @ address, city = @ city, state = @ State, zip = @ zip, contract = @ Contract Where au_id = @ au_id

Selectdetail: select authors. * from authors where au_id = @ au_id

Scalarquery: Select count (*) from authors

Open default. aspx, add a gridview control, and add an objectdatasource control to configure the data source of objecctdatasource. At this time, we will find that the system has recognized the strong dataset we just created during the configuration, [select a Business Object] → [authorstableadapters. authorstableadapter] → [Define data method] Select select, update, insert, and delete respectively, that is, the created deletequery, insertquery, updatequery, selectdetail, scalarquery and getdate generated by the system]. Now the obejctdatasource configuration is complete.

Set the performanceid of the gridview to objectdatasource. Now we have established a complete data editing function. Check whether it is easy.

Of course, we are more accustomed to editing in the background when developing an enterprise. Now I will provide the code for manual editing in the background:

// Data binding part
If (! Page. ispostback)
{
Authorstableadapters. authorstableadapter TA = new authorstableadapters. authorstableadapter ();
// Authorstableadapter is the strongly typed
Gridview1.datasource = TA. getdata ();
Gridview1.databind ();
}
// Delete data
Protected void gridview1_rowdeleting (Object sender, gridviewdeleteeventargs E)
{
Authorstableadapters. authorstableadapter TA = new authorstableadapters. authorstableadapter ();

Ta. deletequery (gridview1.datakeys [E. rowindex]. value );
}
// Return a single piece of data
Authorstableadapters. authorstableadapter TA = new authorstableadapters. authorstableadapter ();
Datatable dt = TA. selectdetail (request. querystring ["ID"]. tostring ());
If (Dt. Rows. Count> 0)
Response. Write (Dt. Rows [0] ["Address"]. tostring ());
// Return data statistics
Authorstableadapters. authorstableadapter TA = new authorstableadapters. authorstableadapter ();
Response. Write (TA. scalarquery (). tostring ());

So far, we can hardly find that 2005 has provided rapid changes to our development. We can easily develop multi-layer architectures and increase the development speed through strong datasets.

 

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.