Enterprise Library-Data Access Application Block-Part 2

Source: Internet
Author: User
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 to add a reference to the namespace: Using Microsoft. Practices. enterpriselibrary. Data; 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 block is complete, the database connection is automatically released. Private VoidMainform_load (ObjectSender, system.EventargsE) { This. Cmbcategory. Items. Clear (); Using(IdatareaderDatareader = _ dB. executereader ("Getcategories")) { // Processing code While(Datareader. Read ()) { CategoryItem =New Category( Datareader. getint32 (0 ), Datareader. getstring (1 ), Datareader. getstring (2 )); This. Cmbcategory. Items. Add (item ); } } If(This. Cmbcategory. Items. Count> 0) This. Cmbcategory. selectedindex = 0; } The following method synchronously updates the product records in the DataGrid when the list box under category is changed. Private VoidCmbcategory_selectedindexchanged (ObjectSender, system.EventargsE) { This. Dsproducts. Clear (); CategorySelectedcategory = (Category)This. Cmbcategory. selecteditem; If(Selectedcategory =Null) Return; _ DB. loaddataset ("Getproductsbycategory", This. Dsproducts, New String[] {"Products"}, Selectedcategory. categoryid ); } The database class provides two methods for filling 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. The following code implements the update method. The btnsave_click method is as follows: Private VoidBtnsave_click (ObjectSender, system.EventargsE) { System. Data. Common.DbcommandInsertcommand =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.DbcommandDeletecommand =Null; Deletecommand = _ dB. getstoredproccommand ("Holdeleteproduct"); _ DB. addinparameter (deletecommand,"Productid", Dbtype. Int32,"Productid",Datarowversion. Current ); _ DB. addinparameter (deletecommand,"Lastupdate", Dbtype. Datetime,"Lastupdate",Datarowversion. Original ); System. Data. Common.DbcommandUpdatecommand =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 ); IntRowsaffected = _ dB. updatedataset ( This. Dsproducts, "Products", Insertcommand, Updatecommand, Deletecommand, Updatebehavior. Standard ); } When updating data, you need to manually create a stored procedure outsourcing layer (wrapper) to map the data table's able field and 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 in this example. Program But you can check whether the database connection needs to be modified: <? XML Version = "1.0"Encoding="UTF-8"?> < Configuration > < Configsections > < Section Name = "Dataconfiguration"Type="Microsoft. Practices. enterpriselibrary. Data. configuration. databasesettings, Microsoft. Practices. enterpriselibrary. Data, version = 4.1.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35"/> </ Configsections > < Dataconfiguration Defaultdatabase = "Quickstarts instance "/> < Connectionstrings > < Add Name = " 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:

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.