27
PART 1 CREATING THE PRODUCT CATALOG

PART 1 CREATING THE PRODUCT CATALOG. ROADMAP FOR THIS CHAPTER To implement the departments list, you’ll start with the database and make your way to the

Embed Size (px)

Citation preview

Page 1: PART 1 CREATING THE PRODUCT CATALOG. ROADMAP FOR THIS CHAPTER To implement the departments list, you’ll start with the database and make your way to the

PA RT 1

CREATING THE PRODUCT CATALOG

Page 2: PART 1 CREATING THE PRODUCT CATALOG. ROADMAP FOR THIS CHAPTER To implement the departments list, you’ll start with the database and make your way to the

ROADMAP FOR THIS CHAPTER

To implement the departments list, you’ll start with the database and make your way to the presentation tier:

1. You’ll create the Department table in the database. This table will store data regarding the store’s departments. Before adding this table, you’ll learn the basic concepts of working with relational databases.

2. You’ll add the GetDepartments stored procedure to the database, which (like all the other stored procedures you’ll write) is logically located in the data tier part of the application.At this step, you’ll learn how to speak with relational databases using SQL.

Page 3: PART 1 CREATING THE PRODUCT CATALOG. ROADMAP FOR THIS CHAPTER To implement the departments list, you’ll start with the database and make your way to the

ROADMAP FOR THIS CHAPTER

3. You’ll create the business tier components of the departments list. You’ll learn how to communicate with the database by calling the stored procedure and sending the resultsto the presentation tier.

4. Finally, you’ll implement the DepartmentsList.ascx Web User Control to display a dynamic list of departments for your visitor, which is the goal of this chapter.

Page 4: PART 1 CREATING THE PRODUCT CATALOG. ROADMAP FOR THIS CHAPTER To implement the departments list, you’ll start with the database and make your way to the
Page 5: PART 1 CREATING THE PRODUCT CATALOG. ROADMAP FOR THIS CHAPTER To implement the departments list, you’ll start with the database and make your way to the

STORING CATALOG INFORMATION

• We’ll briefly discuss the main parts that make up a database table:

• Primary keys• UNIQUE columns• SQL Server data types• Nullable columns and default values• Identity columns• Indexes

Page 6: PART 1 CREATING THE PRODUCT CATALOG. ROADMAP FOR THIS CHAPTER To implement the departments list, you’ll start with the database and make your way to the

INDEXES

You should keep the following in mind about indexes:

• Indexes greatly increase search operations on the database, but they slow down operations

that change the database (delete, update, and insert operations).

• Having too many indexes can slow down the general performance of the database. The

general rule is to set indexes on columns frequently used in WHERE, ORDER BY, and GROUP

BY clauses, used in table joins, or having foreign-key relationships with other tables.

• Indexes are automatically created on primary key and unique table columns.

Page 7: PART 1 CREATING THE PRODUCT CATALOG. ROADMAP FOR THIS CHAPTER To implement the departments list, you’ll start with the database and make your way to the

CREATING THE DEPARTMENT TABLE

Add two sample data

Page 8: PART 1 CREATING THE PRODUCT CATALOG. ROADMAP FOR THIS CHAPTER To implement the departments list, you’ll start with the database and make your way to the

COMMUNICATING WITH THE DATABASE

• The ultimate goal with the table is to get the list of department names from the database using C# code.

• Stored procedures are database objects that store programs written in T-SQL. Much like normal functions, stored procedures accept input and output parameters and have return values.

Page 9: PART 1 CREATING THE PRODUCT CATALOG. ROADMAP FOR THIS CHAPTER To implement the departments list, you’ll start with the database and make your way to the

STORED PROCEDURES ADV.

• Storing SQL code as a stored procedure usually results in better performance because SQL Server generates and caches the stored procedure execution plan when it’s first executed.

• Using stored procedures allows for better maintainability of the data access and manipulation code, which is stored in a central place, and permits easier implementation of the three-tier architecture (the stored procedures forming the data tier).

Page 10: PART 1 CREATING THE PRODUCT CATALOG. ROADMAP FOR THIS CHAPTER To implement the departments list, you’ll start with the database and make your way to the

STORED PROCEDURES ADV.

• Security can be better controlled because SQL Server permits setting different security permissions for each individual stored procedure.

• SQL queries created ad hoc in C# code are more vulnerable to SQL injection attacks, which is a major security threat.

• This might be a matter of taste, but having the SQL logic separated from the C# code keeps the C# code cleaner and easier to manage; it looks better to call the name of a stored procedure than to join strings to create a SQL query to pass to the database.

Page 11: PART 1 CREATING THE PRODUCT CATALOG. ROADMAP FOR THIS CHAPTER To implement the departments list, you’ll start with the database and make your way to the

CREATE THE STORED PROCEDURE

CREATE PROCEDURE GetDepartments ASSELECT DepartmentID, Name, DescriptionFROM Department

Page 12: PART 1 CREATING THE PRODUCT CATALOG. ROADMAP FOR THIS CHAPTER To implement the departments list, you’ll start with the database and make your way to the

ADDING LOGIC TO THE SITE

The business tier (or middle tier) is said to be the brains of the application because it manages the application’s business logic. For the business tier of the departments list, you’ll implement three classes:• GenericDataAccess implements common functionality that you’ll then reuse whenever you need to access the database. Having this kind of generic functionality packed in a separate class saves keystrokes and avoids bugs in the long run.• CatalogAccess contains product catalog–specific functionality, such the GetDepartments method that will retrieve the list of departments from the database.• BalloonShopConfiguration and Utilities contain miscellaneous functionality such as sending emails, which will be reused in various places in BalloonShop.

Page 13: PART 1 CREATING THE PRODUCT CATALOG. ROADMAP FOR THIS CHAPTER To implement the departments list, you’ll start with the database and make your way to the

CHOOSING TECHNOLOGIES AND TOOLS

• No matter which architecture is chosen, a major question that arises in every development project is which technologies, programming languages, and tools are going to be used, bearing in mind that external requirements can seriously limit your options.• The system requirements and software

requirements stages in the software development process will determine which technologies you must use for creating the application.

Page 14: PART 1 CREATING THE PRODUCT CATALOG. ROADMAP FOR THIS CHAPTER To implement the departments list, you’ll start with the database and make your way to the

CONNECTING TO SQL SERVER

• The main challenge is to understand how the code that accesses the database works. The .NET technology that permits accessing a database from C# code is called ADO.NET.

• ADO.NET groups all .NET classes that are related to database access.

• Each database operation always consists of three steps:

1. Open a connection to the SQL Server database.2. Perform the needed operations with the database and get back the results.3. Close the connection to the database.

Page 15: PART 1 CREATING THE PRODUCT CATALOG. ROADMAP FOR THIS CHAPTER To implement the departments list, you’ll start with the database and make your way to the

CONT.

• The class used to connect to SQL Server is SqlConnection. • When creating a new database connection, you

always need to specify at least three important pieces of data:

• The name of the SQL Server instance you’re connecting to• The authentication information that will permit you to access the server• The database you want to work with

Page 16: PART 1 CREATING THE PRODUCT CATALOG. ROADMAP FOR THIS CHAPTER To implement the departments list, you’ll start with the database and make your way to the

EXAMPLE

// Create the connection objectSqlConnection connection = new SqlConnection();// Set the connection stringconnection.ConnectionString = "Server=(local)\SqlExpress; " +"User ID=balloonshop; Password=ecommerce;" +"Database=BalloonShop";// Open the connectionconnection.Open();

Or // Create the connection object and set the connection stringSqlConnection connection = new SqlConnection("... connection string ...");// Open the connectionconnection.Open();

Page 17: PART 1 CREATING THE PRODUCT CATALOG. ROADMAP FOR THIS CHAPTER To implement the departments list, you’ll start with the database and make your way to the

ISSUING COMMANDS AND EXECUTING STORED PROCEDURES

// Create the command objectSqlCommand command = new SqlCommand();command.Connection = connection;command.CommandText = "CatalogGetDepartments";command.CommandType = CommandType.StoredProcedure;

Or

// Create the command objectSqlCommand command = new SqlCommand("GetDepartments", connection);command.CommandType = CommandType.StoredProcedure;

Page 18: PART 1 CREATING THE PRODUCT CATALOG. ROADMAP FOR THIS CHAPTER To implement the departments list, you’ll start with the database and make your way to the

EXECUTING THE COMMAND AND CLOSING THE CONNECTION

• Three Execute methods of the SqlCommand class:

• ExecuteNonQuery• ExecuteScalar• ExecuteReader

Page 19: PART 1 CREATING THE PRODUCT CATALOG. ROADMAP FOR THIS CHAPTER To implement the departments list, you’ll start with the database and make your way to the

EXECUTENONQUERY

• is used to execute a SQL statement or stored procedure that doesn’t return any records. Used when executing operations that update, insert, or delete information in the database. • ExecuteNonQuery returns an integer value that

specifies how many rows were affected by the connection.Open();command.ExecuteNonQuery();command.Close();

Page 20: PART 1 CREATING THE PRODUCT CATALOG. ROADMAP FOR THIS CHAPTER To implement the departments list, you’ll start with the database and make your way to the

EXECUTESCALAR

• is like ExecuteNonQuery in that it returns a single value, although it returns a value that has been read from the database instead of the number of affected rows. It is used in conjunction with SELECT statements that select a single value.

• If SELECT returns more rows and/or more columns, only the first column in the first row is returned.

• E.g: SELECT COUNT(*) FROM

Page 21: PART 1 CREATING THE PRODUCT CATALOG. ROADMAP FOR THIS CHAPTER To implement the departments list, you’ll start with the database and make your way to the

EXECUTEREADER

• is used with SELECT statements that return multiple records

• ExecuteReader returns a SqlDataReader object, which contains the results of the query.

• A SqlDataReader object reads and returns the results one by one, in a forward-only and read-only manner. The good news about the SqlDataReader is that it represents the fastest way to read data from the database, and the bad news is that it needs an open connection to operate—no other database operations can be performed on that connection until the reader is closed.

• Another solution is to load all the data returned by the SqlDataReader into a DataTable object which will allow you to close the database connection very quickly.

Page 22: PART 1 CREATING THE PRODUCT CATALOG. ROADMAP FOR THIS CHAPTER To implement the departments list, you’ll start with the database and make your way to the

EXAMPLE

Here’s a simple example of reading some records from the database and saving them to a DataTable:

// Open the connection

conn.Open();

// Create the SqlDataReader object by executing the command

SqlDataReader reader = comm.ExecuteReader();

// Create a new DataTable and populate it from the SqlDataReader

DataTable table = new DataTable();

table.Load(reader);

// Close the reader and the connection

reader.Close();

conn.Close();

Page 23: PART 1 CREATING THE PRODUCT CATALOG. ROADMAP FOR THIS CHAPTER To implement the departments list, you’ll start with the database and make your way to the

IMPLEMENTING GENERIC DATA ACCESS CODE

// Create a new database provider factoryDbProviderFactory factory =DbProviderFactories.GetFactory("System.Data.SqlClient");// Create the connection objectDbConnection conn = factory.CreateConnection();// Initialize the connection stringconn.ConnectionString = "... connection string ...";// Create the command object and set its propertiesDbCommand comm = conn.CreateCommand();comm.CommandText = "CatalogGetDepartments";comm.CommandType = CommandType.StoredProcedure;// Open the connectionconn.Open();// Execute the command and save the results in a DataTableDbDataReader reader = comm.ExecuteReader();DataTable table = new DataTable();table.Load(reader);// Close the reader and the connectionreader.Close();conn.Close();

Page 24: PART 1 CREATING THE PRODUCT CATALOG. ROADMAP FOR THIS CHAPTER To implement the departments list, you’ll start with the database and make your way to the

CATCHING AND HANDLING EXCEPTIONS

• The general strategy to deal with runtime exceptions is as follows:

• If the error is not critical, deal with it in code, allowing the code to continue executingnormally, and the visitor will never know an error happened.• If the error is critical, handle it partially with code to reduce the negative effects as muchas possible, and then let the error propagate to the presentation tier that will show thevisitor a nice-looking “Houston, we have a problem” page.• For the errors that you can’t anticipate, the last line of defense is still the presentationtier, which logs the error and politely asks the visitor to come back later.

Page 25: PART 1 CREATING THE PRODUCT CATALOG. ROADMAP FOR THIS CHAPTER To implement the departments list, you’ll start with the database and make your way to the

TRY – CATCH – FINALLY CONSTRUCT

try{// code that might generate an exception}catch (Exception ex){// code that is executed only in case of an exception// (exception's details are accessible through the ex object)//it is optional}finally{// code that executes at the end, no matter if// an exception was generated or not//it is optional}

Page 26: PART 1 CREATING THE PRODUCT CATALOG. ROADMAP FOR THIS CHAPTER To implement the departments list, you’ll start with the database and make your way to the

SENDING EMAILS

• To send emails, you need the SmtpClient and MailMessage classes from the System.Net.Mail namespace.• MailMessage has four important properties that

you set before sending an email: From, To, Subject, and Body.• When working with SmtpClient, you can set its

Host property to the address of an external SMTP server; otherwise, the mail is sent through the local SMTP service in Windows.

Page 27: PART 1 CREATING THE PRODUCT CATALOG. ROADMAP FOR THIS CHAPTER To implement the departments list, you’ll start with the database and make your way to the

START PRACTICE SHEET 2