Yesterday Insus. NET has written a blog article "WEB controls do not what the so-called good, but use good" http://www.cnblogs.com/insus/p/3821644.html many netizens disagree with each other, Insus. NET has also made a personal reply in the comments. For details, see #29 and #30.
In this article, Insus. NET continues to be developed with Web controls. If you are not interested in Web controls, you can only say sorry, but you can skip it.
The database uses Access as an example. Insus. NET seems to have never used Access data in commercial use, all of which are SQL Server. It doesn't matter. everything is the same.
Next, we will add this table on the asp.net website, and perform a series of operations such as getting data, updating, and deleting. These operations may be repeated every day, because they are the most basic functional operations. It is like eating and walking every day.
First, insert data to the database. On the. aspx html markup webpage, pull a few TextBox text boxes and a Button:
You can switch to the Add event ButtonAdd_Click () on the. aspx. cs write button (). Because Item_nbr is a unique primary key, you need to judge whether it already exists before adding it. If the record does not exist, insert the record. This requires the question that comes to mind when writing a program.
First, let's see how to write it?
If the Item_nbr field value already exists in the database. The program prompts and returns the Add event. Otherwise, it will continue to run the following program:
OleDbReader and OleDbCommand are applied to determine and execute the inserted SQL statement. After the data is stored in the database, we need to display the desired data instead of letting the user directly view the tables in the database.
On the. aspx webpage, pull a GridView control:
When the web page is displayed, the data is also displayed:
On the asp.net web page, we can maintain data. If you add data, you can write an error, edit the update, or delete some records. On the GridView control, add a column and set the DataKeyNames attribute:
The Program for deleting a method is as follows:
If you want to update the data, it is the same as the program that obtains and deletes the data, except that the input SQL statement is different. The following function is used to obtain a single field value for a record:
Event programs can be referred:
Well, all the above functions are intended to be expressed in Insus. NET this time.
To be honest, when Insus. NET was learning and using. NET Framework2.0 to write asp.net just a decade ago, I did not know how much effort and time it took to write it out. Now let's go back and write it.
Demonstrate all the above functions in Real Time:
After the demo is completed, let's look at the code you just wrote. Two lines of code appear in each method or associated operation with the database:
By the way, these can be written to the Web. in the config file, you can directly go to the Web when changing the database. the configuration file does not need to be modified on every page. Every function is found and changed, which is easy to be missed or wrong.
In this way, all the two lines of code can be changed:
Insus. NET uses the Replace () method to Replace the database path.
All the above implementations are two layers, page-level.
This is just some exercises. Next, you want to add another table to the database to store other data. Such as members and orders. The processing of each table basically requires the above functions, such as adding, updating, and deleting. The method you think of, follow the above method to re-write a copy. A better way is to copy and rename the webpage above. Then modify the SQL statement.
When there are more and more requirements for these functions, each time you add one, copy one and change one. When some code is repeatedly done, the web page is constantly larger, making it difficult to maintain. Therefore, we will naturally think of how to reconstruct and simplify them.
OK. Because of the strong coupling of these codes, we can create an object, such as BizBase, when Learning Object-oriented, and move the code that processes the database to the newly created object. Start with the Data_Binding () method, which is the method for getting data from the database.
Only the highlighted block code is stable, and the Red Arrow is two variables. We can define these two variables as attributes in BizBase.
Then, create the stability code as a method:
Let's take a look at how the method of this object is used in Data_Binding:
With this encapsulation, the three layers have already come out. The code for processing logic has already run into the BizBase class. In. aspx. cs, we only need to pass SQL statements that change frequently. Next, we will continue to implement the deletion method, identify and insert functions, and move the stable code to the BizBase object.
With this category, we can rewrite the code originally written, or create a new webpage, as long as it involves the SELECT, INSERT, UPDATE, or DELETE with the database, you can use the above method.
This can also be considered as the logic layer (basic) we often call.
To reconstruct and modify the webpage, first determine whether the record exists:
The method for adding records to the database is as follows:
The Code reconstruction of the delete method is as follows:
The method for getting scalar value is reconstructed as follows:
Just now, after a series of major rectifications, let's move it over and take a break to grab Coffee. Rest is to take a longer journey.
Now we can see the code we wrote on the. aspx. cs webpage, or the two sentences are repeated in a few places:
string mdb = System.Web.Configuration.WebConfigurationManager.AppSettings["mdb"];string _connectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["InsusConnectionString"].ConnectionString.Replace("mdb", Server.MapPath(mdb));
In this way, I can also move them to the BizBase category:
Write it on the constructor public BizBase () and rewrite the two sentences into one sentence. Since we have moved this connectionstring to the BizBase class, the ConnectionString attribute in this category is removed:
In the. aspx. cs webpage, the following code has become redundant, meaning it is not too late. delete them and purify our code:
Here, I believe you know more about how our logical class library is generated. No matter how many tables you need to add, delete, update, or obtain data (dataset or scalar value), you only need to instantiate the BizBase class, input SQL statements, and execute related methods.
This article demonstrates the source program (Access DB;. NET Framework 4.0 ;):
Http://download.cnblogs.com/insus/ASPDOTNET/Asp_OOP.rar
Note: The article is too long to be written as a sequent. Please pay attention to it ......