Create a web application with C #

zhaozj2021-02-11  154

Using Microsoft is being implemented. NET technology and C # languages ​​can quickly establish web applications, and their security and upgradeability are greatly better than ordinary ASP applications. In this article, we will use it. NET and C # create an application in one step. System Requirements: Internet Explorer 5.5 Windows 2000 Professional, Server or Advanced Server ASP / Microsoft. Net (preview can be downloaded in http://msdn.microsoft.com/net) SQL Server 7.0 SP1 or later using Microsoft. NET's Advanced Object Model quickly creates a secure, easy to upgrade web application. Microsoft. Net is an ideal technology for developing business solutions. . NET technology combines C #'s elasticity and high performance, and developing business procedures is simpler than previous methods that use only ASPs in the past. In this article, we will use it. Net and C # to create a simple business app - a program that allows customers to browse your product catalog. This program contains the most basic. NET and C # technology, in this article, it will involve how to establish and compile C # commercial application layer components. Also talk about how to use the ASPX page access component and binding data. Integrated use of these technologies, which can also cost the web application less than the traditional ASP method. Stable. NET applications still rely on a stable database plan and carefully prepredible stored procedure. The application can call the written stored procedure through the component. Most multi-layer methods do not recommend calling data directly from the program, which will reduce the speed of the program and is not debugging. This article mainly discusses C # and ASP , but there are more discussion to establish databases and stored procedures. The development of components We will be written as a logical layer and data access layer simultaneously. If you intend to transplant the program into another database, such as Oracle, you need to put the logical layer and the data access layer in both components, respectively. This article uses only SQL Server, so you don't need to separate them. This application requires two pages: Default.aspx and ProductList.aspx. Default.aspx is the page that the user visits the web first, which lists all the types of products. When the user selects the type from the Default.aspx menu, the user will enter the ProductList.aspx page, which shows a list of all products of the current class. During the user using these two pages, two actions have occurred: The first is to list the product type (Default.aspx) in the menu, and the second is listed in the page of the product list (ProductList. ASPX). We can build two functions to complete these two tasks, both functions acquire data by calling stored procedures. Corresponding to these two functions, we build two classes in a name space called CommercedotNet: Category and Product. (Figure 1) Instantiate the Categories class using CommercedotNet.categories, instantiate the Products class using CommerceDotNet.Products. In order to clear the code, we declare these two classes in different files. Since they share a namespace, even in different files declare, they will become a separate component after compiling. The categoryList method passes a data set to all product types.

The method of adding CategoryList in the Categories class. The code is as follows: Namespace CommercedotNet {public class categories {public dataset categorylist () {}}} After the class is created, it starts to add code to the CategoryList method. CategoryList method gets data from the process of four steps: 1. Set a data connection and command object via SqlConnection and SqlDataSetCommand objects. 2. Set the command object type to the stored procedure. 3. Send the result of the stored procedure ListCategory into the dataset. 4. Return the data set containing the result to the function of calling it. Complete CategoryList method code follows: public DataSet CategoryList () {// establish a data connection and command objects SQLConnection myConnection = new SQLConnection ( "server = localhost; uid = sa; pwd =; database = commercedotnet"); SQLDataSetCommand myCommand = new SQLDataSetCommand ( "ListCategory", myConnection); // set the command object type stored procedure myCommand.SelectCommand.CommandType = CommandType.StoredProcedure; // create and fill the dataset dataSet myDataSet = new dataSet (); myCommand.FillDataSet (myDataSet, "CategoryList "); // Returns the data set return mydatanet;} The ProductSList method is similar, but the productSlist passes a parameter to the storage process.

Code is as follows: public DataSet ProductsList (int categoryID) {// establish a data connection and command objects SQLConnection myConnection = new SQLConnection ( "server = localhost; uid = sa; pwd =; database = commercedotnet"); SQLDataSetCommand myCommand = new SQLDataSetCommand ( " ListProducts ", myConnection); // set the command object type stored procedure myCommand.SelectCommand.CommandType = CommandType.StoredProcedure; // transmitted to the stored procedure parameter SQLParameter parameterCategoryID = new SQLParameter (" @ CategoryID ", SQLDataType.Int, 4); parameterCategoryID.Value = categoryID; myCommand.SelectCommand.Parameters.Add (parameterCategoryID); // create and fill the dataset dataSet myDataSet = new dataSet (); myCommand.FillDataSet (myDataSet, "Products"); // return data sets return myDataSet Two classes have been established, and after the compiler. Use commands: CSC /OUT:../bin/commercedotnet.dll / t: library /r :system.data.dll categorydb.cs productdb.cs "/ out:" Switch specifies the compiling library name and storage location, "/ T: "Switch tells the compiler to establish library," / R: "The switch indicates other libraries involved in components, and the complete source program is listed later. After the development of the ASP page is established, the following work is to develop as a user interface ASP page. In the front part, we designed the default.aspx and productslist.aspx pages, the Default.aspx page is the first page after the site is loaded, and we start with the establishment of this default page. When the page is read, the Page_Load () method is triggered, this method completes the data obtained from the database and binds to myList's task. First, declare an iCollection type variable MenuItems. Then, instantiate the component, call the CategoryList method, and put the data set in the MenuiteMs object. Set the data source of MyList to menuitems. Execute myList.DATABIND method to bind the data to myList.

This code is as follows: