A small application for data access in the enterprise database

Source: Internet
Author: User
Friends who have used Microsoft's Enterprise Library should be able to feel the convenience and high-speed development experience that application block brings to us,
This is especially true for dataaccessapplicationblock, which simplifies public methods for data access and is suitable for multiple
Application scenarios, such as reading data for display, transferring data between application layers, or submitting updated data to the database.
Blocks support stored procedures and inline SQL statements. At the same time, they compress applications that manage common tasks such as managing connection objects, creating and caching parameters.
Use Program Method body. In other words, the Data Access Application Block provides the frequently used feature of ado.net in the simplified class, promoting
Development efficiency.
Needless to say, the following describes an example of using dataset to update a database. This example uses the offline model function in ado.net.
1. Prepare a set of stored procedures for addition, deletion, and modification, Code As follows: Create   Procedure Addproduct
(
@ Productname   Nvarchar ( 50 ),
@ Categoryid   Int ,
@ Unitprice   Money
)
As

Insert   Into Products (productname, categoryid, unitprice)
Values ( @ Productname , @ Categoryid , @ Unitprice )

Select Productid, productname, categoryid, unitprice
From Products
Where Productid =   Scope_identity ()
Go

Create   Procedure Deleteproduct
(
@ Productid   Int
)
As

Delete Products
Where Productid =   @ Productid
Go

Create   Procedure Updateproduct
(
@ Productid   Int ,
@ Productname   Nvarchar ( 50 ),
@ Lastupdate   Datetime
)
As

Update Products
Set Productname =   @ Productname
Where Productid =   @ Productid   And Lastupdate =   @ Lastupdate
 
If   @ Rowcount   >   0
-- This statement is used to update the dataset if changes are done on the updated record (identities, timestamps or triggers)
Select Productid, productname, categoryid, unitprice
From Products
Where Productid =   @ Productid
Go

2. Create database operation functions Database DB = Databasefactory. createdatabase (); // Use the default connection string Configuration
// Initialize dataset and fill in data
Dataset productsdataset =   New Dataset ();

String Sqlcommand =   " Select productid, productname, categoryid, unitprice, lastupdate "   +
" From Products " ;
Dbcommand = DB. getsqlstringcommand (sqlcommand );

String Productstable =   " Products " ;
DB. loaddataset (dbcommand, productsdataset, productstable );


// Get datatable from Dataset
Datatable table = Productsdataset. Tables [productstable];

// Add a new value for a row
Datarow addedrow = Table. Rows. Add ( New   Object [] {Dbnull. value,"New Product",11,25} );

// Modify the value of a record
Table. Rows [ 0 ] [ " Productname " ] =   " Modified Product " ;

// Create a dbcommand object and add, delete, and bind the modified stored procedure to the dataset to be updated.
Dbcommand insertcommand = DB. getstoredproccommand ( " Addproduct " );
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 );

Dbcommand deletecommand = DB. getstoredproccommand ( " Deleteproduct " );
DB. addinparameter (deletecommand, " Productid " , Dbtype. int32, " Productid " , Datarowversion. Current );

Dbcommand updatecommand = DB. getstoredproccommand ( " Updateproduct " );
DB. addinparameter (updatecommand, " Productid " , Dbtype. int32, " Productid " , Datarowversion. Current );
DB. addinparameter (updatecommand, " Productname " , Dbtype. string, " Productname " , Datarowversion. Current );
DB. addinparameter (updatecommand, " Lastupdate " , Dbtype. datetime, " Lastupdate " , Datarowversion. Current );

// Submit dataset and automatically call the stored procedure to update related records in the database.
Int Rowsaffected = DB. updatedataset (productsdataset, " Products " , Insertcommand, updatecommand,
Deletecommand, Microsoft. Practices. enterpriselibrary. dataaccess. updatebehavior. Standard );

This method does not need to actively call the add, delete, or modify stored procedures. Instead, it is assigned to the dbcommand object based on the submitted dataset.
Automatic comparison is used to determine when to call the API. This is a must for family members.
We hope you will have more discussions about the Enterprise Library so that our development will be more fun.

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.