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

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

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

570 Chapter 13 • Creating a Message Board with ADO and XML

the primary key. BoardName is a Text field, with Required set to Yes, and with a Field Size of 100. BoardDescription is a Text field, with Field Size set to the maximum access allows, which is 255.

Figure 13.1 The Boards Table

The Threads table will also contain four fields:ThreadID,ThreadSubject, CreatorID, and BoardID.ThreadID should be an AutoNumber, with Indexed set to Yes (No Duplicates), and should also be the primary key.ThreadSubject is a Text field, with Field Size set to the maximum access allows, which is 255. CreatorID is a Number, with Field Size set to Long Integer, and Required set to Yes. BoardID is a Number, with Field Size set to Long Integer, and Required set to Yes (see Figure 13.2).

The Posts table will contain six fields: PostID, PostSubject, PostBody, CreatorID,ThreadID, and PostDate. PostID should be an AutoNumber, with Indexed set to Yes (No Duplicates), and should also be the primary key. PostSubject is a Text field, with Field Size set to the maximum access allows, which is 255. PostBody is a Memo field with Required set to Yes. CreatorID is a Number, with Field Size set to Long Integer, and Required set to Yes. ThreadID is a Number, with Field Size set to Long Integer, and Required set to Yes. PostDate will be a Date/Time field with a Default Value of Now() and Required set to Yes. See Figure 13.3 for the Posts table.

www.syngress.com

Creating a Message Board with ADO and XML • Chapter 13

571

Figure 13.2 The Threads Table

Figure 13.3 The Posts Table

The Users table will contain eight fields: UserID, Username, Password, FirstName, LastName, Email, IsAdmin, IsBanned. UserID should be an

www.syngress.com

572 Chapter 13 • Creating a Message Board with ADO and XML

AutoNumber, with Indexed set to Yes (No Duplicates), and should also be the primary key. Username is a Text field, with its Field Size set to 50 and Required set to Yes. Password is a Text field, with its Field Size set to 50 and Required set to Yes. FirstName is a Text field, with its Field Size set to 100 and Required set to Yes. LastName is a Text field, with its Field Size set to 200 and Required set to Yes. Email is a Text field, with its Field Size set to 255 and Required set to Yes. IsAdmin and IsBanned are Yes/No fields, with Format set to True/False and Required set to Yes. See Figure 13.4 for the Users table.

Figure 13.4 The Users Table

The last step is to define the relationships between the tables. Posts relates to Threads on ThreadID.Threads relates to Board on BoardID. Users relates to Posts on CreatorID, to Threads on CreatorID, and Board on ModeratorID (see Figure 13.5).

SQL Server Database

Setting up a SQL Server database is rather effortless, especially since you can let the database do everything for you by executing a SQL script.The only thing you need to do is open up your SQL Enterprise Manager, navigate to the server you want to create your database on, and open up the Databases node. Right-click the Databases node and select New Database. Name your database dotBoard and select OK.

www.syngress.com

Creating a Message Board with ADO and XML • Chapter 13

573

Figure 13.5 The Relationships between Tables

The only other action you need to take is open up SQL Query Analyzer, and execute the SQL Script shown in Figure 13.6 (which can also be found on your CD, called dotBoard Setup.sql).

Figure 13.6 SQL Server Database Creation Script (dotBoard Setup.sql)

CREATE TABLE [dbo].[Board] (

[BoardID] [int] IDENTITY (1, 1) NOT NULL ,

[BoardName] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,

[BoardDescription] [varchar] (255) COLLATE SQL_Latin1_General_ CP1_CI_AS NOT NULL

) ON [PRIMARY] GO

CREATE TABLE [dbo].[Posts] (

[PostID] [int] IDENTITY (1, 1) NOT NULL ,

[PostSubject] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,

[PostBody] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,

Continued

www.syngress.com

574 Chapter 13 • Creating a Message Board with ADO and XML

Figure 13.6 Continued

 

[CreatorID] [int] NOT NULL ,

 

 

[ThreadID] [int] NOT NULL ,

 

 

[PostDate] [datetime] NOT NULL DEFAULT getDate()

) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

 

GO

 

 

CREATE TABLE [dbo].[Threads] (

 

 

[ThreadID] [int] IDENTITY (1, 1) NOT NULL ,

 

[ThreadSubject] [varchar] (255) COLLATE SQL_Latin1_General_

 

CP1_CI_AS NOT NULL ,

 

 

[CreatorID] [int] NOT NULL ,

 

 

[BoardID] [int] NOT NULL

 

) ON

[PRIMARY]

 

GO

 

 

CREATE TABLE [dbo].[Users] (

 

 

[UserID] [int] IDENTITY (1, 1) NOT NULL ,

 

[Username] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT

 

NULL ,

 

 

[FirstName] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS

 

NOT NULL ,

 

 

[LastName] [varchar] (200) COLLATE

SQL_Latin1_General_CP1_CI_AS

NOT

 

 

 

NULL ,

 

 

[Email] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT

 

NULL ,

 

 

[IsAdmin] [bit] NOT NULL ,

 

 

[IsBanned] [bit] NOT NULL

 

) ON

[PRIMARY]

 

GO

 

 

ALTER TABLE [dbo].[Board] WITH NOCHECK ADD

 

CONSTRAINT [PK_Board] PRIMARY KEY

CLUSTERED

 

(

 

[BoardID] ) ON [PRIMARY]

Continued

www.syngress.com

Creating a Message Board with ADO and XML • Chapter 13

575

Figure 13.6 Continued

GO

ALTER TABLE [dbo].[Posts] WITH NOCHECK ADD

CONSTRAINT [PK_Posts] PRIMARY KEY CLUSTERED

(

[PostID] ) ON [PRIMARY]

GO

ALTER TABLE [dbo].[Threads] WITH NOCHECK ADD

CONSTRAINT [PK_Threads] PRIMARY KEY CLUSTERED

(

[ThreadID] ) ON [PRIMARY]

GO

ALTER TABLE [dbo].[Users] WITH NOCHECK ADD

CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED

(

[UserID] ) ON [PRIMARY]

GO

ALTER TABLE [dbo].[Users] WITH NOCHECK ADD

CONSTRAINT [DF_Users_IsAdmin] DEFAULT (0) FOR [IsAdmin],

CONSTRAINT [DF_Users_IsBanned] DEFAULT (0) FOR [IsBanned]

GO

ALTER TABLE [dbo].[Posts] ADD

CONSTRAINT [FK_Posts_Threads] FOREIGN KEY

(

[ThreadID]

) REFERENCES [dbo].[Threads] ( [ThreadID]

) ON DELETE CASCADE ON UPDATE CASCADE , CONSTRAINT [FK_Posts_Users] FOREIGN KEY

(

[CreatorID]

Continued

www.syngress.com

576 Chapter 13 • Creating a Message Board with ADO and XML

Figure 13.6 Continued

) REFERENCES [dbo].[Users] ( [UserID]

)

GO

ALTER TABLE [dbo].[Threads] ADD

CONSTRAINT [FK_Threads_Board] FOREIGN KEY

(

[BoardID]

) REFERENCES [dbo].[Board] ( [BoardID]

) ON DELETE CASCADE ON UPDATE CASCADE , CONSTRAINT [FK_Threads_Users] FOREIGN KEY

(

[CreatorID]

) REFERENCES [dbo].[Users] ( [UserID]

)

GO

Lastly, go back to your SQL Enterprise Manager, navigate to your database, and select the Diagrams node. Right-click and select New Database Diagram. Click Next, then select our four database tables and hit Next.The diagram should be created automatically for us and should look a lot nicer than the MS Access version. (see Figure 13.7).

Designing Your Application

When designing an application, there are two possible main routes.The first is the procedural approach (anyone familiar with ASP 3.0 and earlier who did not use COM objects to handle logic knows exactly what I mean): the simple “Page1” does this,“Page2” does this, and so on.You have a set of “top-down” ASP scripts (that is, your code starts at the top and executes until it hits the bottom), with functions including files, which make up your application.There is technically nothing wrong with this approach, as there are many large-scale applications that are written exactly this way.

www.syngress.com

Creating a Message Board with ADO and XML • Chapter 13

577

Figure 13.7 The SQL Server Diagram

Your other choice is to take a more Object-Oriented (OO) approach. In an OO world, you create a set of classes and interfaces that make up the core of your application. Using these classes, you create a user interface that will define what an average user would consider your “application.”This approach allows the designer of the classes to encapsulate a good majority of all the code and logic behind the application, without exposing it to whomever might be building the user interface (and likely, the same person will be building both). An OO approach also allows your application to be used in a variety of ways, and would allow someone to build multiple user interfaces on top of the exact same set of classes.

Both approaches have their merits and flaws.With the procedural approach, you will be stuck in ASP.NET for your user interface, and if you want to “copy” logic from one place to another, you either have to create globally scoped functions, or copy and paste code.The procedural approach does tend to be a bit easier to create, though, because you do not have the additional overhead of having to create classes to handle your logic and data.The Object-Oriented approach effectively encapsulates your entire application into a small set of classes, grouped into an assembly .dll file, which can be created from another application and used.This allows you to hand another developer your assembly file, and let him or her go about building the actual user interface without you ever needing to know what the user interface was.The drawback to building an application in

www.syngress.com

578 Chapter 13 • Creating a Message Board with ADO and XML

an OO-manner is whomever is developing the classes needs to take a lot of care to get it done properly, and be able to build it in such a manner as to not tie it exclusively to one type of user interface.

When deciding whether or not dotBoard should be procedural or ObjectOriented, take into account these things. First and foremost, you need to be able to maintain your code. If your code is modularized into multiple functions and organized very well, then the procedural approach doesn’t seem too bad. However, if your code is placed haphazardly throughout your application, finding your bugs and improving code at a later date might be harder. If your code is organized into a set of classes and public interfaces (the methods and procedures that an application can “see”), it is typically easier to maintain your code, as each piece of each class is a very small piece of the application as a whole, and making changes won’t likely take a large amount of code.

The other thing you should think about is that dotBoard is being written in VB.NET. For anyone who has built an application in straight ASP, you would probably be more comfortable with the procedural approach. For anyone who has built an application in ASP and created VB COM objects, you would most likely be more comfortable with the Object-Oriented approach, but feel some trepidation about speed and performance issues. For the master gurus out there who are building C++ ATL COM objects and using them in ASP, you might scoff at VB.NET and think you would rather stick with C++ ATL COM.Well, all of you have very valid points. A straight procedural approach is generally looked down upon in a professional environment,VB COM objects in ASP are typically regarded as slow and frequently memoryand processor-intensive, and well, nobody can read the C++ ATL code anyway, so it doesn’t count!

Seriously, though, every point made is very valid about every technology discussed.That’s where VB.NET comes in.The .NET runtime is remarkably fast. The Just-In-Time (JIT) compilation of your code only happens the first time it is executed; so, after that initial execution, your code runs incredibly fast until you change it (at which point the JIT compilation happens again).VB.NET is also a fully Object-Oriented language. It provides developers with every good OO technique available, and it is actually quite easy to write OO applications with it.

All that said, it’s pretty obvious dotBoard should be an Object-Oriented application. Don’t worry if you’ve never written any OO code before. ObjectOriented techniques are relatively easy to implement, and even if you don’t think you’ve ever used any objects before, you probably have (especially if you’ve done any development in ASP).

www.syngress.com

Creating a Message Board with ADO and XML • Chapter 13

579

Designing Your Objects

Now that we’ve decided on object orientation, we need to analyze our application and determine what our objects will “look like.” At this point, you might say, “we’ve already done that while analyzing and building our database,” and you’d be right.We have already done that. Half of our design work is now already done! The only other part we have to do is map the data we’ve already analyzed to VB.NET types and group them accordingly.We’re going to do that with the wonder of UML (Unified Modeling Language). If you don’t know what UML is, don’t worry; all we’re using it for here is to show you some pretty pictures of what our classes are going to look like. Please note that all of these objects and all files will be found on the CD that accompanies this book.

Creating Your Data Access Object

To make it easier for each of your objects to have access to the database, we’re going to create a singular data access object that does everything for you.We’re going to call this class DataControl, and it is going to be comprised of solely shared methods. A shared method means you do not need to create an instance of a User object to call it. DataControl will contain two Shared methods, GetDataSet and ExecuteNonQuery. GetDataSet returns a DataSet, and ExecuteNonQuery executes a SQL statement and returns nothing.This class is pretty straightforward, and is shown in Figure 13.8 (likewise, it can also be found on your CD as DataControl.vb).

Figure 13.8 DataControl.vb

Imports System.Data

Imports System.Data.OleDb

Imports System.Web

Imports System.Configuration

Imports System.Collections.Specialized

Public Class DataControl

Public Shared Function GetDataSet(ByVal SQL As String) As DataSet Dim connectionString As String

Dim settings As ConfigurationSettings

Dim appSettings As NameValueCollection

Continued

www.syngress.com