Web| Program | To create the use of Microsoft is being implemented. NET technology and the C # language can quickly build Web applications that are much more secure and upgradeable than ordinary ASP applications. In this article, we will use. NET and C # to build an application step-by-step.
System Requirements:
Internet Explorer 5.5
Windows Professional, server or Advanced server
Asp+/microsoft. NET (preview version can be downloaded in http://msdn.microsoft.com/net)
SQL Server 7.0 + SP1 or later
Use Microsoft. NET's Advanced object models (Advanced object model) can quickly establish secure, easily upgraded Web applications.
Microsoft's. NET is an ideal technology for developing business solutions. NET technology combined with C # 's flexibility and high performance, the development of business processes is simpler than the previous approach of using ASP only.
In this article, we will use. NET and C # to create a simple business application-a program that allows customers to browse your product catalog.
This program contains the most basic. NET and C # technology, in this article, how to build and compile the commercial application layer components of C #. You'll also talk about how to use ASPX pages to access components and bind data. Combined with these technologies, you can spend less time building Web applications than traditional ASP methods.
Solid. NET applications still rely on robust database planning and well-written stored procedures.
An application can invoke a stored procedure that has already been written through a component. Most multi-tier methods do not recommend directly from the program to call the data, which will reduce the speed of the program, but also not conducive to debugging.
This article mainly discusses C # and asp+, not too much discussion of the establishment of database and stored procedures.
Development of components
We will write the C # component as both a logical layer and a data access layer.
If you plan to migrate your programs to another database, such as Oracle, then you need to place the logical layer and the data access layer in two separate components. Only SQL Server is used in this article, so you don't need to separate them.
This application requires two pages: Default.aspx and ProductList.aspx. Default.aspx is the first page that users visit to the Web, and it lists all kinds of products. When a user selects a category from the Default.aspx menu, it goes to the ProductList.aspx page, which shows a list of all the products in the current class.
When users use these two pages, two actions occur: The first is to list the product category (Default.aspx) in the menu, and the second is to list the products (productlist.aspx) on the page of the product list. We can create two functions to accomplish both tasks, both of which get the data by invoking the stored procedure. In response to these two functions, we set up two classes in a namespace called Commercedotnet: Category and Product. (Figure 1) instantiate the Categories class using Commercedotnet.categories, and use Commercedotnet.products to instantiate the products. To make the code clear, we declare these two classes separately in different files. Since they share a namespace, even in different file declarations, they will become a separate component after compilation.
The CategoryList method passes a data set containing all product categories to the system. Add the CategoryList method to the Categories class.
The code is as follows:
Namespace Commercedotnet {
public class Categories {
Public DataSet categorylist () {
}
}
}
After you set up this class, you start adding code to the CategoryList method.
The process of obtaining data by CategoryList method is divided into four steps: 1. Establish data connections and command objects through SqlConnection and Sqldatasetcommand objects. 2. Set the Command object type to a stored procedure. 3. Listcategory the result of the stored procedure into the dataset. 4. Return the data set containing the result to the function that called it.
The complete CategoryList method code is as follows:
After two classes are established, compile the program.
Use command: csc/out:. /bin/commercedotnet.dll/t:library/r:system.data.dll CategoryDB.cs ProductDB.cs
The "/OUT:" switch specifies the name and location of the compilation library, the "/t:" switch tells the compiler to set up a library, the "/r:" Switch indicates the other libraries involved in the component, and the complete source program is listed later in the end of the article.
The development of asp+ page
After you have set up your components, the next task is to develop a asp+ page as a user interface. In the previous section, we designed the Default.aspx and productslist.aspx pages, and the Default.aspx page is the first page after the site is loaded, and we start by setting up this default page.
When the page is read, the Page_Load () method is triggered, which completes the data obtained from the database and binds to the MyList task. First, declare a ICollection type variable MenuItems. Then, instantiate the component, call the CategoryList method, and store the dataset in the MenuItems object. Set the MyList data source to MenuItems. Executes the Mylist.databind method to bind the data to the mylist.
This part of the code is as follows:
<script language= "C #" runat= "Server" >
void Page_Load (Object sender, EventArgs e) {
ICollection MenuItems;
Commercedotnet.categories Categories = new Commercedotnet.categories ();
Other code in Default.aspx is similar to a traditional ASP page, see the code listed at the end of this article.
ProductList.aspx is similar to default.aspx, but the code that binds the data has changed because, in developing the C # component, we passed parameters to the stored procedure in the Commercedotnet.productslist method, so the asp+ page also needs a phase should have changed.
Commercedotnet.products products = new commercedotnet.products ();
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