Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

ASP .NET Web Developer s Guide - Mesbah Ahmed, Chris Garrett

.pdf
Скачиваний:
38
Добавлен:
24.05.2014
Размер:
7.32 Mб
Скачать

510 Chapter 12 • Creating an ADO.NET Shopping Cart

SQL Server Database

Now that we have our schema upsized into SQL, we can easily create the rest of our database components.We primarily need a set of stored procedures that will run all of our operations against the database.This will enable us not to have to use ad hoc queries in our code for our Data Tier interaction.

One thing we need to do first is ensure that all our primary keys were transcribed into the upsized version. Let’s open up the Enterprise Manager of MSSQL 2000 (EM). Navigate to your program files and select the SQL Server group, then select EM. From EM, we can quickly navigate to our database (shopDB). See Figure 12.7.

Figure 12.7 The SQL EM Interface

Select Tables and you’ll notice our tables from Access are now here. Rightclick a table and select Design Table. From here, we can check to see if our tables made the move without ill effects. If everything looks correct, check the rest of the tables—you’ll see the Access datatype “autonumber” does not come over to SQL Server as an “int” identity column datatype, which it needs to be. So, for the tables that have autonumber, you will have to change it to the “int” datatype with identity, and give them a seed and increment value. See Figure 12.8.

www.syngress.com

Creating an ADO.NET Shopping Cart • Chapter 12

511

Figure 12.8 Setting Identity to Yes and Giving Seed and Increment Value

You must also uncheck Allow Nulls.This is because the field we are working with is a primary key and we cannot have a null value for a primary key field. Also, we are using the option identity in this instance, which requires that null not be allowed.

We will also separate the table “BookOrders” into its original design since SQL Server can easily give us a value for the identity field returned. After we have done all of this, we can create a new diagram in SQL and apply our new relationships. In the EM view, right-click diagrams and select New Diagram. The wizard will prompt you for the tables you want to select for the database diagram. Add only the tables we have created, leave out all the system tables.We will now view our new diagram generated by SQL Server (see Figure 12.9).

We can create relationships in the same manner as before. Click the column you want to make a relationship with and drag and drop it into to the appropriate column and table.We will go with the selected defaults.We have a normalized database now completed in SQL Server.We will now create the stored procedures (procs) we’ll need for the rest of the application.

www.syngress.com

512 Chapter 12 • Creating an ADO.NET Shopping Cart

Figure 12.9 A SQL Server Diagram

Creating the Stored Procedures

We’ll now create the following list of stored procedures:

AdminAddBook

AdminAddCustomer

AdminAddCat

AdminDeleteCat

AdminDeleteCustomer

AdminDeleteBook

AdminUpdateBook

AdminUpdateCat

AdminUpdateCustomer

AllCustById

www.syngress.com

Creating an ADO.NET Shopping Cart • Chapter 12

513

GetAllBooks

GetAllCat

LoginCustomers

OrderBook

Don’t be intimidated.We’ll use the SQL Server Wizard to create most of these procedures. Now we need to begin creating all our stored procedures. Go to the Tools menu and select Wizards. From there, a new window will pop up with a listing of items. Double-click the first item, Database, then select Create Stored Procedure Wizard.You should see the screen shown in Figure 12.10.

Figure 12.10 The Create Stored Procedure Wizard

Click Next and select the database, which is shopDb.The next window will show all the tables on the left and the subsequent procedures that can be created on the right. Mark the check box labeled insert in the row of options listed for the Customers table. Click Next.The window that appears will give you the choice to edit the SQL syntax—select this option.We need to give the procedure a name, which in this case will be AdminAddCustomer. See Figure 12.11.

In Figure 12.11, we see that all columns are selected for insert; however, we do not need one for CT_ID because the identity field generates that. Uncheck that option and rename the proc AdminAddCustomer. Select Edit SQL. Let’s look at the code generated by this; it’s shown in Figure 12.12 and found on the CD as ShopDB.sql.

www.syngress.com

514 Chapter 12 • Creating an ADO.NET Shopping Cart

Figure 12.11 The Stored Procedure Wizard’s Properties Dialog Box

Figure 12.12 ShopDB.sql

USE [shopDb]

GO

CREATE PROCEDURE [AdminAddCustomer] (@CT_FirstName [nvarchar](20), @CT_LastName [nvarchar](50), @CT_Email [nvarchar](75), @CT_Password [nvarchar](6))

AS INSERT INTO [shopDb].[dbo].[Customers] ([CT_FirstName],

[CT_LastName], [CT_Email], [CT_Password])

VALUES (@CT_FirstName,

@CT_LastName, @CT_Email, @CT_Password)

www.syngress.com

Creating an ADO.NET Shopping Cart • Chapter 12

515

Here we have the SQL syntax to insert a row of new data. In the code view window, SQL Server likes to put numbers on all the variables.You can delete this so the code looks cleaner and will be easy to use when we write the Web service that will hit this proc and execute it. Create the rest of the Admin procs in this same manner.

Now that we have completed a majority of the stored procedures needed for our database through the use of the wizards, we have to create more complex stored procedures using the Query Analyzer. Open up Query Analyzer from the Tools menu of EM. Connect the server you are running. In the drop-down menu, select the database shopDb.The next proc we need to build is AllCustById.We will write a simple select statement with one parameter.

Let’s look at some code which can be executed in Query Analyzer:

CREATE PROC AllCustById @CT_ID int

AS

SELECT *

FROM customers

WHERE CT_ID = @CT_ID

GO

The next procedure in the list after AllCustById is GetAllBooks. No need for parameters—just give up the data.

CREATE PROCEDURE GetAllBooks

AS

SELECT BK_ISBN isbn, category.CAT_Name "name", category.CAT_ID "id", BK_ImagePath imgSrc, BK_author author, BK_Price price,

BK_Title title,

BK_Description "description"

FROM Books book inner Join Categories category on book.CAT_ID = category.CAT_ID

www.syngress.com

516Chapter 12 • Creating an ADO.NET Shopping Cart

ORDER BY "name"

NOTE

In the code in this section, we are using aliasing so the column headers returned will have easy-to-use names. The DataSet will use the column names as XML element names when the data is converted to XML.

Now we need to get a selection of categories from the database for our dropdown menus:

CREATE PROC GetAllCat

AS

SELECT * FROM Categories

This will populate with all category names and associated IDs.

Now we need to create a proc that will query the database and return a Customer’s ID.This is our Login proc:

CREATE proc LoginCustomers @CT_Email nvarchar(75), @CT_Password nvarchar(6)

as

SELECT [CT_ID]

FROM Customers

WHERE CT_Email = @CT_Email And CT_Password = @CT_Password

This will return a value of either the Customers ID or –1, which we can check for on the page load.

Now we need to handle the ordering of a book.We can load and run the OrderBook procedure to do that:

CREATE Procedure OrderBook

(

@CT_ID int,

www.syngress.com

Creating an ADO.NET Shopping Cart • Chapter 12

517

@BK_ISBN int, @BKOR_Quantity int, @BKOR_Price money

)

AS

declare @OR_Date datetime declare @OR_ShipDate datetime declare @OR_ID int

select @OR_Date = getdate() select @OR_ShipDate = getdate()

begin tran NewBook

INSERT INTO Orders

(

CT_ID,

OR_Date,

OR_ShipDate

)

VALUES

(

@CT_ID, @OR_Date, @OR_ShipDate

)

SELECT @OR_ID = @@Identity

INSERT INTO BookOrders

(

OR_ID,

BK_ISBN,

www.syngress.com

518 Chapter 12 • Creating an ADO.NET Shopping Cart

BKOR_Quantity,

BKOR_Price

)

VALUES

(

@OR_ID, @BK_ISBN, @BKOR_Quantity, @BKOR_Price

)

commit tran NewBook

We are using begin tran and end tran.This simply means that if there is an error during any part of the previous query the transaction will be aborted and rolled back.That’s it for the stored procedures. Now to make these all work in the Access DB, we need to trim out some stuff from the preceding code.

As a rule of thumb, we can grab all the code after the key word AS.This is then pasted into Access query SQL mode and saved as the same file name. Open up the shopDB.mdb file and see the differences in the code.

Creating the Web Services

This section will provide an overview of the Web Services needed for our site, and describe the processes of creating the data connection, creating a Web Service, and, finally, testing the Web Service.

Overview of the Book Shop Web Services

We will be using Web Service methods to wrap our database logic (stored procedures for SQL, or parameterized queries for Access).This will provide separation of the data tier from the UI.This will also enable our data to be accessed from multiple clients including Java-servlets, JSP, PHP, desktop application with Hypertext Transfer Protocol (HTTP) connections, and, of course,ASP.NET applications.

We will be creating the following Web Services (see Figure 12.13):

sellerAdmin

adminCustomer

getCustomer

www.syngress.com

Creating an ADO.NET Shopping Cart • Chapter 12

519

loginCustomer

getBooks

getCategories

orderBooks

Figure 12.13 An Overview of Web Services and Their Methods

 

 

 

 

 

 

 

 

 

 

 

getCust

 

 

adminCustomer

 

 

 

 

 

 

 

 

 

 

 

allCustById

 

 

 

addCust

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

removeCust

 

 

 

 

loginCustomer

 

 

updateCust

 

 

 

 

 

 

 

 

 

 

 

 

validCustomer

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

sellerAdmin

 

 

 

getCategories

 

 

 

 

 

 

 

 

 

 

 

 

 

 

addItem

 

 

 

 

allCat

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

removeItem

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

getBooks

 

 

updateItem

 

 

 

 

 

 

 

 

 

 

 

 

allBooks

 

 

 

 

 

 

 

 

 

 

 

addCat

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

removeCat

 

 

 

 

orderBooks

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

orderItem

 

 

 

updateCat

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Earlier in this chapter (see the section “Setting Up the Database”), we created stored procedures for use with an SQL database, as well as the equivalent parameterized queries for use with an Access database, to make the interface to the data source consistent; this allows us to write ADO.NET code that can be used against both SQL and Access.

We will also use the OleDb data connection object since most databases have an OleDb provider.This will enable our code not only to work with SQL and Access but with any database that has an OleDb interface. So, our application will work with an SQL database and our application will work with an Access database. And the only code that will need to be changed with this approach is the connection string.

Let’s create a new project to host all our Web Services. Open Visual Studio

.NET Beta 2 (VS.NET), and select New Project.We want to create a C# ASP.NET Web Service application named “booksource” (see Figure 12.14); next, we will create the data connection.

www.syngress.com