Enterprise Library-Data Access Application Block learning Manual (Latest Version)-Part 2

Source: Internet
Author: User
Tags connectionstrings
Enterprise Library. net Framework 3.5-entlib v4.1 is the patterns & Practices Group. net Framework 3.5 is used to develop a set of Enterprise libraries. The latest version is v4.1, which includes 9 Application blocks, including data access application blocks and Exception Handling application blocks) validation Application Block is very helpful and practical for enterprise application development.
Http://blog.entlib.com/EntLib/archive/2009/03/30/enterprise-library-for-.net-framework-3.5-entlib-v4.1-v4.1.aspx previous: Enterprise Library-Data Access Application Block learning Manual (the latest version)-Part 1 this article according to entlib v4.1 learning Manual (hands on Lab ), demonstrate data access stored procedures and update data records. Before starting, run the following SQL script to create related data tables and stored procedures: categories under the Enterprise Library 4.1 Hol \ CS \ data access \ exercises \ ex02 \ dbsetup directory. SQL and holsps. SQL. 1. Now we can open the project \ Enterprise Library 4.1 Hol \ CS \ data access \ exercises \ ex02 \ begin \ dataex2.sln to start our work. Open mainform. CS code file, add reference to namespace: using Microsoft. practices. enterpriselibrary. create the following private database variable in form: Private Database _ DB = databasefactory. createdatabase ("quickstarts instance"); 2. compile the code in the mainform_load method and write the following code. Call the getcategories stored procedure and return the datareader data object. After the using statement block is complete, the database connection is automatically released. Private void mainform_load (Object sender, system. eventargs e) {This. cmbcategory. items. clear (); Using (idatareader datareader = _ dB. executereader ("getcategories") {// processing code while (datareader. read () {category item = new category (datareader. getint32 (0), datareader. getstring (1), datareader. getstring (2); this. cmbcategory. items. add (item) ;}} if (this. cmbcategory. items. count> 0) This. c Mbcategory. selectedindex = 0;} The method below synchronously updates the product records in the DataGrid when the list box of category is changed. Private void cmbcategory_selectedindexchanged (Object sender, system. eventargs e) {This. dsproducts. clear (); Category selectedcategory = (Category) This. cmbcategory. selecteditem; If (selectedcategory = NULL) return; _ dB. loaddataset ("getproductsbycategory", this. dsproducts, new string [] {"Products"}, selectedcategory. categoryid);} the database class provides two methods to fill the dataset: executedataset and loaddataset. The executedataset method returns a newly created DataSet object. The loaddataset method fills in an existing DataSet object. The first parameter passed in the above loaddataset method is the name of the getproductsbycategory stored procedure, and the second parameter is the dataset variable to be filled, the third parameter is to map the results returned by a stored procedure to a specified set of table names. The value of the last parameter is passed to the stored procedure parameter value, which is categoryid. Next we will implement the update method. The code of the btnsave_click method is as follows: Private void btnsave_click (Object sender, system. eventargs e) {system. data. common. dbcommand insertcommand = NULL; insertcommand = _ dB. getstoredproccommand ("holaddproduct"); _ dB. addinparameter (insertcommand, "productname", dbtype. string, "productname", datarowversion. current); _ dB. addinparameter (insertcommand, "categoryid", dbtype. int32, "categoryid", datarowversion. Current); _ dB. addinparameter (insertcommand, "unitprice", dbtype. currency, "unitprice", datarowversion. current); system. data. common. dbcommand deletecommand = NULL; deletecommand = _ dB. getstoredproccommand ("holdeleteproduct"); _ dB. addinparameter (deletecommand, "productid", dbtype. int32, "productid", datarowversion. current); _ dB. addinparameter (deletecommand, "lastupdate", dbtype. datetime, "lastupdat E ", datarowversion. original); system. data. common. dbcommand updatecommand = NULL; updatecommand = _ dB. getstoredproccommand ("holupdateproduct"); _ dB. addinparameter (updatecommand, "productid", dbtype. int32, "productid", datarowversion. current); _ dB. addinparameter (updatecommand, "productname", dbtype. string, "productname", datarowversion. current); _ dB. addinparameter (updatecommand, "categoryid", dbtype. Int32, "categoryid", datarowversion. current); _ dB. addinparameter (updatecommand, "unitprice", dbtype. currency, "unitprice", datarowversion. current); _ dB. addinparameter (updatecommand, "lastupdate", dbtype. datetime, "lastupdate", datarowversion. current); int rowsaffected = _ dB. updatedataset (this. dsproducts, "Products", insertcommand, updatecommand, deletecommand, updatebehavior. standard);} when updating data, you must You must manually create an outsourcing layer (wrapper) for a stored procedure to map the data table's able field to the stored procedure parameters. The database-class updatedataset method is rarely called. We will not go into detail here. 3. Check the configuration file app. config. The configuration file of this sample program has been created. However, you can check whether the database connection needs to be modified: <? Xmlversion = "1.0" encoding = "UTF-8"?> <Configuration> <configsections> <sectionname = "dataconfiguration" type = "Microsoft. practices. enterpriselibrary. data. configuration. databasesettings, Microsoft. practices. enterpriselibrary. data, version = 4.1.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35 "/> </configsections> <dataconfigurationdefadatabase database = "Quickstarts instance"/> <Connectionstrings> <addname =" Quickstarts instance"Connectionstring =" database = entlibquickstarts; server = (local); Integrated Security = sspi; "providername =" system. data. sqlclient "/> </connectionstrings> </configuration> 4. run the sample program. The running interface of the sample program is as follows: Welcome to continue accessing the subsequent content.

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.