Using strongly typed datasets in VS2005 simplifies development

Source: Internet
Author: User
Tags count insert new features query return tostring zip visual studio
In the daily development, in order to write data to increase, update, modify, delete and other functions and have to face the boring code, do repetitive and repetitive work. The release of the official release of the. NET 2.0 is a great event for our program developers, Visual Studio 2005 Some of the new enhancements and new features of Ado.net 2.0 make it easier and faster to develop highly scalable multi-tier database applications.

Strong-type datasets can be used under. NET 1.1, but there are some inconvenient developments in Visual Studio 2003, which have greatly improved the design view of the dataset in Visual Studio 2005 because the Ado.net 2.0 adds a TableAdapter object, so you can now add a DataTable's table adapter to the DataSet Design view. The previous typeddatasets was just a collection of data entities (dumb objects, only attributes), and it was not easy to manage the persistence of self-management and data integrity validation. Now it's convenient to implement these.

Below is an example to detail how to generate a strongly typed dataset through vs2005 to simplify the development process and generate scalable multi-tier database applications.

First create a new Web site project, add a new item]→[dataset in Solution Explorer]→[, name Authors.xsd, because the strongly typed dataset needs to be placed in the App_Code directory, if there is no App_Code directory in the project file, The IDE prompts you to create, and we choose Yes.

The IDE automatically opens the Authors.xsd file we just built and starts running the TableAdapter Configuration Wizard, and if Web.config doesn't have a database connection string, then we need to create a new connection: select New link]→[ The Microsoft SQL server]→[select system's own pubs database]→[saves the newly created link string to the application configuration file].

In the next selection of command types, the IDE comes out with three options for the user to select the form of the database:

1, using SQL statements, if it is a single table, the wizard can automatically generate insert,update and DELETE statements. This example is also used in this method.

2. Create a new stored procedure. Similarly, if you are a single table, the wizard can automatically generate insert,update and DELETE statements.

3. Use existing stored procedures. You need to select the appropriate existing stored procedure for each command.

We choose to [Use SQL statements]→[Query Builder]→[authors tables, select all Columns], at which point the Query Builder automatically generates SQL statements (select authors.* from authors). [Advanced options], you can choose whether to generate insert,update and DELETE statements, use optimistic concurrency to prevent concurrent conflicts, refresh the datasheet to validate the INSERT and UPDATE statements, and in order to be able to introduce the use of the method more clearly, we all do not choose, And let's add it to our needs later. [Select the method to generate], we only need to use [return DataTable], the rest of the options check out.

The Create Wizard automatically generates a SELECT statement, a map of the datasheet, and a Get method. The process we created is over. This is what we found dataset.xsd will automatically generate a authors table and a dataset containing the Get method Authorstableadapter.

Similarly, in our established data table [Authorstableadapter] right → add query, refer to the method above to generate DeleteQuery (delete), InsertQuery (add), UpdateQuery (update), Selectdetail (Returns a detailed result, uses the Select (Return row) option, generates a dataset that includes only one piece of data), and scalarquery (returns data statistics).

The following are the generated SQL statements that need to be manually modified to 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 a ObjectDataSource control to configure the Objecctdatasource data source. At this point we will find that the system has identified the strongly typed DataSet we have just established, and [select the business object]→[authorstableadapters.authorstableadapter]→[define the data method] Choose Select Individually, Update,insert,delete method, that we have just established deletequery,insertquery,updatequery,selectdetail,scalarquery and system generated getdate]. The configuration Obejctdatasource is now complete.

The DataSourceID of the GridView is set to ObjectDataSource, at which point we have established a complete data editing function. See if it's easy.

Of course, when we are doing enterprise development, we are more accustomed to editing in the background, and now I give the code for manual editing in the background:

Data Binding Section
if (! Page.IsPostBack)
{
Authorstableadapters.authorstableadapter ta = new Authorstableadapters.authorstableadapter ();
Authorstableadapter is the strong type we built.
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 ());
At this point, we are not difficult to find that 2005 to our development provides a leap of change. We can use strongly typed datasets to facilitate the rapid development of multi-tier architectures and improve development speed.



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.