In my opinion, the objectdatasource of all the functions added by Microsoft in Asp.net is indeed one of the most eye-catching things for me, and it is not like the feeling of chicken ribs given by the previous datasource control, it brings us a fresh feeling in the three-tier database development process.
To make good use of objectdatasource, we must first agree with Microsoft's visualization concept in a three-tier development model. Worms think that objectdatasource is a bridge between the three-layer data architecture widely used by Microsoft and the visual programming that Ms has always advocated. However, according to the personal usage of worms, this bridge basically satisfies the data provision services (such as gradview, datalist, detailsview, and formview) of various database controls launched by Ms ), objectdatasource provides data exchange between the business logic layer and the user interface layer directly on the page.
The following is a little thing that I made some time ago:
First, let's talk about the three-layer structure. Between the business logic layer and the user interface layer, in the previous Asp.net 1.1, the user interface layer must use third-party controls or directly read and write data from the business logic layer, however, you cannot bind control data. You must manually enter the data required for interfaces in the business logic layer, the program is programmed according to the format of the page control used to present the data obtained from the business logic layer one by one. This development model was taken for granted in the Development of Asp.net in the past. To use a three-tier architecture, we must sacrifice development efficiency. However, at present, Ms is accompanied by the introduction of objectdatasource, which loudly says no!
So let's take a look at the new development process after objectdatasource is introduced in the current three-tier architecture? First, in the database service layer, we do not need to make any changes. We continue to use the database interfaces that we think have been optimized for a long time. If you prefer dataset, continue; if you prefer to use sqldataadapter, continue. There is no need for any changes. Similarly, in the business logic layer, even though you write as expected, you do not need to change anything, such as when reading data, no matter whether you return dataset or an array or list defined by yourself, it is possible to bring about a massive change at the user interface layer, bugs often implement code-free programming (C # or basic) on the user interface layer. net code), so how is this done?
Here, a simple example is used to describe the bug:
Assume that a bookstore's background management program must add, delete, change, and query the category of a book. For the database layer, it is nothing more than writing these four stored procedures: insert, update, delete, select, and calling their methods. In the business logic layer, you must first define a class, such as class bookclass, and then write the following four functions based on the four stored procedures:
Class bookclass
{
Public static int insert (...); // Adds the category of a book and returns the ID of the book.
Public static bool Update (...); // Modify the category of a book. If yes, true is returned. If no, Flash is returned.
Public static bool Delete (); // Delete the category of a book. If yes, true is returned. If no, Flash is returned.
Public static dataset select (); // query the category information of a book under the specified condition. A dataset is returned successfully, and null is used for failure.
}
Note that the parameter name in your function must be consistent with the field name of the table in the database or the parameter name of the stored procedure. If it is a primary key, the parameter name must be: original _ <field Name>.
After writing the class code of books in the business logic layer above, the following is how to call the code. In the development process of the traditional Asp.net 1.1, if you want to add a book category, you must manually enter insert (...) The parameters in the function are processed based on the returned values. What about now?
Here comes the way for the worm to be lazy:
Drag a formview on the page, select new data srouce from chose data srouce in the Wizard, select database, and set the ID to objdata (Remember, dataasource is not an object, do not ask why), and then define the required fields step by step as prompted, at last, IDE automatically generates the formview template design in the required format. Then, you can delete the sqldatasource control that was automatically added (do not ask me why), and drag an objectdatasource, change the objectdatasource ID to the ID: objdata of the sqldatasource control you just deleted, select the objdata setting wizard, select configure data srouce, and set it to bookclass, select, insert, delete, and update are used to set the functions you have defined in the class bookclass. Then, if you do not need other functions, simply finish them. If it is a SELECT query and it is a parameter, you can set it according to the corresponding entries. Then, press "F5" to run it. Haha. As for page layout and beautification, what should I do? It's a job of the artist. I can't go over the other day.
Congratulations, you have completed this program. Haha, no code programming. Because of the time, the minor bugs will sort out a small piece of code for your reference.
You can use objectdatasource to set the data directly. Bind the required control for each field. Note that if it is a primary key in a data table, we recommend that you add it to the datkey of formview or gradview. Similarly, the variable in the function must be original _ <field Name>.
A worm that has holes in Mircosoft trees
Contact mail: Code Worm
The current home page of the worm: http://www.gofordesign.com