ASP.net 2.0 easy to implement database application development

Source: Internet
Author: User
Tags bind implement connect new features sort versions web services connectionstrings
asp.net| Data | database

Objective:

The first time the use of ASP.net 2.0 was attracted by its powerful features and high efficiency, the complex process was simplified to an unbelievable ease, and according to Microsoft, the December Chinese version of the VS2005 Official edition will bring us a stronger storm. On the ASP.net 2.0 gradually in-depth, it produced a want to find their own learning problems and experience to communicate with you, hence the text. Disclaimer: All of the demo examples in this article are based on the Visual web Developer the beta2,visual of the Developer Express Edition Beta2 is a simple, easy-to-use development tool that focuses Focus on Internet application development.

1. ASP.net 2.0 new features for database development:

Compared with previous versions, ASP.net 2.0 adds more features, making the developer's productivity, management, scalability, and performance areas vastly improved.

In development efficiency, ASP.net 2.0 has reduced the amount of code for a typical Web application by almost 70%, even in simple programs where you can implement the functionality you require, called "0 code," without writing any code. This is because ASP.net 2.0 encapsulates common Web tasks into application services and controls that can be easily reused between Web sites. With these basic building blocks, the customization code needed to implement many applications now is much less than previous versions.

At the same time, many new server controls have been introduced in ASP.net 2.0 to provide powerful declarative support for data access, login security, wizard navigation, menus, tree views, portals, and more. Many of these controls leverage ASP.net's core application services for data access, membership and roles, and personalization settings.

The new features in version 2.0 are as follows:

· Data source Control--asp.net 2.0 introduces declarative data source controls that expose data from back-end storage, such as SQL databases, middle-tier business objects, or XML files, to data-bound UI controls on the page. Data source controls also expose features such as sorting, paging, caching, updating, inserting, and deleting data, and UI controls can automatically take advantage of these features without needing code.

· New data-bound controls--in addition to the data-bound controls in the ASP.net 1.x version, ASP.net includes new data-bound UI controls such as GridView, DetailsView, FormView, TreeView, and Menu, which you can customize to formats to display data. The GridView, DetailsView, and FormView controls also automate the use of data source functionality, making it easy to add sorting, paging, and update operations to data-driven pages.

· Data Control Parameters--the data source can accept input parameters from various sources using the new data Control parameter object in ASP.net 2.0. You can use these parameter objects to easily provide server control property values, sessions, applications, cookies, and QueryString fields, and user profile properties to parameterized data operations. With these parameters, you can support filtering and master/detail scenarios with very little custom code or without custom code.

· Improved data-binding syntax-the DATABINDER.EVAL data-binding syntax in asp.net is simplified for common scenarios that bind controls in a data-bound template. You can also associate bidirectional data binding with control properties in a template to allow you to automatically pass a value to a data source update, insert, or delete operation. For hierarchical XML data, ASP.net 2.0 also contains an Xpath based data binding syntax.

· Local file database using SQL Express--to simplify development, ASP.net 2.0 now supports the ability to connect to a SQL Express database as a local file in an application, eliminating the need to provide a database to the server for development purposes only. Of course, you can still connect to a server-based SQL database.

The following is a detailed description of some of the important data-bound controls and data source controls:

· Gridview:

This is a new data-bound control in ASP.net 2.0, which is a successor to the DataGrid control in ASP.net 1.x, and is based on a number of improvements. Although the DataGrid control in ASP.net 1.x has been able to satisfy developers with code to handle some operations, such as sorting, paging, editing and deleting data. However, when a Gridviwe control is bound to a data source control, it can automatically sort, page, edit, and delete a series of processing data. Functionally gridviwe controls also go beyond the DataGrid control, Gridviwe controls can support multiple key fields, have powerful user-customization capabilities on the UI interface, and it gives developers a new model for canceling and handling events.

· SqlDataSource:

The SqlDataSource control represents a data source control that is connected to a SQL database provider through Ado.net. Like other data source controls, SqlDataSource controls can be bound to any data-bound control that supports DataSourceID properties. At the same time, SqlDataSource has the ability to describe itself (such as selecting, inserting, updating, deleting, sorting, etc.) so that data-bound controls can automatically select, INSERT, UPDATE, delete, sort, and so on when these capabilities are allowed. The SqlDataSource control allows you to replace a large amount of ado.net code that was previously required when creating a database connection and implementing a query because the query operation of the data has been directly specified as a property of the data source control. Because the data query still exists in the page's code, sometimes we call it a double model.

SqlDataSource controls are typically targeted at small and personal sites that do not need to query a large number of databases, and when we develop a Web site with a large data flow, we must use the following ObjectDataSource controls, This is a data source control for large enterprise development that is bound to a middle-tier object.
 Data source Control

Data source controls do not render, but rather represent specific backend data stores, such as databases, business objects, XML files, or XML Web services. Data source controls also support rich functionality for data, such as sorting, paging, filtering, updating, deleting, and inserting, which can be used automatically by data-bound UI controls. ASP.net 2.0 includes the following out-of-the-box server controls:

Name Description
SqlDataSource Supports binding to ado.net providers, such as Microsoft? SQL Server, OLE DB, ODBC, or Oracle. ) represents the SQL database.
ObjectDataSource Supports binding to middle-tier objects, such as data access tiers or business components.
AccessDataSource Support for binding to Microsoft? Access (Jet) database.
SiteMapDataSource Supports the hierarchy that is bound to the ASP.net 2.0 site navigation provider exposure.
XmlDataSource Supports binding to XML files or documents.


  Data-bound controls

A data-bound control is a UI control that renders data as markup to a requesting client device or browser. A data-bound control can automatically bind to data exposed from a data source and get the data at the appropriate time in the page request life cycle. These controls also have the option to take advantage of the data source features, such as sorting, paging, filtering, updating, deleting, and inserting. A data-bound control connects to a data source control through its DataSourceID property. You may be familiar with some data-bound controls in the ASP.net 1.x version, such as the DataGrid, DataList, Repeater, and list controls such as DropDownList. ASP.net 2.0 also contains several new data-bound controls, such as:

Name Description
Gridview Renders data in a grid format. This control is the evolution of the DataGrid control and can automatically take advantage of the data source functionality.
DetailsView To render a single data item in a table of label/value pairs, similar to Microsoft? The Form view in Access. This control can also automatically take advantage of the data source functionality.
FormView Renders a single data item at a time in the form defined by the custom template. To render a single data item in a table of label/value pairs, similar to Microsoft? The Form view in Access. This control can also automatically take advantage of the data source functionality.
Treeview Renders data in a hierarchical tree view of an expandable node.
Menu Renders data in a layered dynamic menu, including a pop-up menu.


Below we combine the GridView and SqlDataSource two controls to implement some of the simplest data operations. I'll give you a detailed example and explain it in detail.

This is the simplest data-driven page that contains only read-only reports, and it shows the data in the database, but does not allow us to change individual data items. First we have to set up a SqlDataSource control and then connect to a database bound control, in this case we use the GridView to specify the DataSourceID property of the GridView as above SqlDataSource.

The authors table of the pubs database in SQL Server 2000, which is structured as follows:


The ConnectionString property of the SqlDataSource control specifies a database connection string, and the SelectCommand property is the database command that specifies that the retrieval be performed. The connection string can be specified directly in the page code, or a new expression structure can be used to retrieve the database connection string in Web.config.

The first step is to drag a "GridView" control onto the main page under the data item in the toolbox, and then a small label appears after you place the mouse on the control for a short pause:


In the second step, we open the Drop-down list of select data sources as shown in the following illustration:


Step three: If you have not set any SqlDataSource, you can select the "New Data source" option. After the interface with the following diagram appears:

We chose "Database" as our data source. Automatically generates a SqlDataSource data source control with the id "SqlDataSource1".

Step Fourth: When a SqlDataSource data source control is generated on the main page, it also displays the following window:


Click the "New Connection" button to display the following form:


"Server name" is an instance of your local SQL Server, and you can choose 2 ways to log on to the server:

Windows authentication and SQL Server validation are two, and the differences I'll explain in detail later. We choose Windows Authentication here First, click "Test Connection" after selecting "pubs" database, if the display connection is successful, it indicates that we have connected to the SQL Server server.

Fifth step: When you click the OK button, the form "Configure Data source" is displayed, and we select "Hoowoo.pubs.dbo" as the database we are going to connect to.

Step sixth: Because we want to save the connection string to the application configuration file, we want to keep the connection string in Web.config. Here we want to save the connection string as an alias: Pubs


Step Seventh: Select the "Authors" table below to select the data we need, where we have selected all the fields.

After completing the above steps, we select "View Code" on the right menu on the main page to see the following code:

<%@ Page language= "C #"%>
<title> GridView Bound Fields </title>
<body>
<form id= "Form1" runat= "Server"
<asp:gridview id= "GridView1" datasourceid= "SqlDataSource1" autogeneratecolumns= "False" runat= "Server"
<Columns>
<asp:boundfield headertext= "ID" datafield= "au_id" readonly= "true"/>
<asp:boundfield headertext= "Last Name" datafield= "au_lname"/>
<asp:boundfield headertext= "Name" datafield= "au_fname"/>
<asp:boundfield headertext= "Phone" datafield= "phone"/>
<asp:boundfield headertext= "Address" datafield= "address"/>
<asp:boundfield headertext= "City" datafield= "City"
<asp:boundfield headertext= ' state ' datafield= ' state '/>
<asp:boundfield headertext= "Zip Code" datafield= "zip"/>
<asp:checkboxfield headertext= "Contract" datafield= "Contract"
</Columns>
</asp:GridView>
<asp:sqldatasource id= "SqlDataSource1" runat= "Server" selectcommand= "SELECT [au_id], [au_lname], [au_fname], [phone], [Address],[city], [state], [zip], [contract] from [authors] "connectionstring=" <%$ connectionstrings:pubs%> "/>
</form>
</body>

The code in Web.config is as follows:

>









Now to focus on the meaning of these codes:

" autogeneratecolumns= "False" runat= "Server" ""

Data-bound controls connect to data source controls through their DataSourceID properties, allowing us to sort, page, filter, update, delete, and insert a series of actions.

The







the

"BoundField" and "CheckBoxField" are the types of controls to bind, "HeaderText" is the name of the field that will be displayed on the table, and "DataField" is the data field that we want to bind.

Selectcommand= "SELECT [au_id], [au_lname], [au_fname],
[Phone],[address],[city], [state], [zip], [contract] from [authors]
connectionstring= "<%$ connectionstrings:pubs%>"/>

In the SqlDataSource control we set the database's SelectCommand command as "SELECT [Au_id],[au_lname],[au_fname],[phone],[address] [city], [state], [Zip], [contract] from [authors] this exactly corresponds to the control one by one that the GridView is bound to, which fully illustrates the close connection between a data-bound control and a data source control.

Attentive readers may be surprised, connectionstring= "<%$ connectionstrings:pubs%> in SqlDataSource is what?" This question has a lot to do with why we need Web.config configuration files. The following nodes are set up in Web.config:




We can get the database connection string alias by retrieving the Web.config configuration file the real meaning of "Pubs" is
"Data source=hoowoo;initial catalog=pubs;integrated security=true" providername= "System.Data.SqlClient" Initial Catalog that we are using the "pubs" database. Integrated security explains how we use Windows authentication.

The final display is as follows:



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.