Duwamish online e-bookstore Homepage

Source: Internet
Author: User
The duwamish bookstore uses the data storage mode used by dataadapter and Dataset. what is different is that it performs subclass extension on dataset as a data carrier, that is, custom dataset is used for data transmission between layers. The following is a custom dataset example:

Public class bookdata: Dataset
{
Public bookdata ()
{
//
// Create the tables in the dataset
//
Builddatatables ();
}
Private void builddatatables ()
{
//
// Create the books table
//
Datatable table = new datatable (books_table );
Datacolumncollection columns = table. columns;

Columns. Add (pkid_field, typeof (system. int32 ));
Columns. Add (type_id_field, typeof (system. int32 ));
Columns. Add (publisher_id_field, typeof (system. int32 ));
Columns. Add (publication_year_field, typeof (system. int16 ));
Columns. Add (isbn_field, typeof (system. String ));
Columns. Add (image_file_spec_field, typeof (system. String ));
Columns. Add (title_field, typeof (system. String ));
Columns. Add (description_field, typeof (system. String ));
Columns. Add (unit_price_field, typeof (system. decimal ));
Columns. Add (unit_cost_field, typeof (system. decimal ));
Columns. Add (item_type_field, typeof (system. String ));
Columns. Add (publisher_name_field, typeof (system. String ));

This. Tables. Add (table );
}
.........
}
Bind the customized books table With This dataset ..

In duwamish, there are five classes in the data layer: books, categories, MERS MERs, and orders. Each class is only responsible for data access. The following is the sample code of one of the classes:
Private sqldataadapter dscommand;
Public bookdata getbookbyid (INT bookid)
{
Return fillbookdata ("getbookbyid", "@ bookid", bookid. tostring ());
}
Private bookdata fillbookdata (string commandtext, string paramname, string paramvalue)
{
If (dscommand = NULL)
{
Throw new system. objectdisposedexception (GetType (). fullname );
}
Bookdata DATA = new bookdata ();
Sqlcommand command = dscommand. selectcommand;

Command. commandtext = commandtext;
Command. commandtype = commandtype. storedprocedure; // use stored proc for perf
Sqlparameter Param = new sqlparameter (paramname, sqldbtype. nvarchar, 255 );
Param. value = paramvalue;
Command. Parameters. Add (PARAM );

Dscommand. Fill (data );
Return data;
}

The upper layer basically performs some strict Data Validity verification (of course, some complicated transaction logic will be included, but not much). The sample code is as follows:
Public customerdata getcustomerbyemail (string emailaddress, string password)
{
//
// Check preconditions
//
Applicationassert. checkcondition (emailaddress! = String. Empty, "email address is required ",
Applicationassert. linenumber );
Applicationassert. checkcondition (password! = String. Empty, "password is required ",
Applicationassert. linenumber );
//
// Get the customer Dataset
//
Customerdata dataset;
Using (dataaccess. Customers customersdataaccess = new dataaccess. Customers ())
{
Dataset = customersdataaccess. loadcustomerbyemail (emailaddress );
}
//
// Verify the customer's password
//
Datarowcollection rows = dataset. Tables [customerdata. customers_table]. Rows;

If (rows. Count = 1) & rows [0] [customerdata. password_field]. Equals (password ))
{
Return dataset;
}
Else
{
Return NULL;
}
}

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.