Read Microsoft Visual C# 2005 Express Edition: Build a Program Now! Online

Authors: Patrice Pelland

Tags: #General, #Computers, #C♯ (Computer program language), #Programming Languages, #C#, #Microsoft .NET Framework, #Computer Books: Languages, #Computer Graphics, #Application software, #C# (Computer program language), #Programming, #Microsoft Visual C# .NET, #Microsoft Visual C♯ .NET, #Electronic books, #Game Programming & Design, #Computing: Professional & Programming, #C (Computer program language), #Computers - Languages, #Programming Languages - C#, #Programming & scripting languages: general

Microsoft Visual C# 2005 Express Edition: Build a Program Now! (29 page)

BOOK: Microsoft Visual C# 2005 Express Edition: Build a Program Now!
7.36Mb size Format: txt, pdf, ePub
ads

C07622299.indd 124

C07622299.indd 124

10/24/05 7:14:01 PM

10/24/05 7:14:01 PM

There are some useful tabs that you didn’t use in this debugging session. For instance, the Watch tab is important because you can enter variables and expressions that you want to follow and monitor during the execution of the application.

Another useful tab is the Immediate window, where you can type anything and the compiler verifies, compiles, and executes it on the fly! Any effect on the application under debug is immediate. Any piece of code that can be evaluated by the compiler and does not require a block of code can be entered into the Immediate window. You could enter a loop, for instance. You also have full access to IntelliSense in this window just as if you were in the code editor. Let’s look at a simple example.

TO USE THE IMMEDIATE WINDOW

1 Put a breakpoint at the first instruction in your application and run the application by pressing
F5
. If you don’t see the Immediate window, just select the
Debug
menu and then select
Windows
and 2
Immediate
. You should have an empty Immediate window in the bottom of your screen. 3 Type the following line and press Enter:
int i = 5

4 Now type this line and press Enter:
MessageBox.Show(i.ToString());
The message box that appears should show a 5. You can test code in real time during the execution without executing a single line of code from your application. But beware if you use variables that are in your application; remember that if you modify them in the Immediate window you modify them for the application as well. 5 Click the
Stop Debugging
button to stop executing the application. You’re now not only able to build new applications, but you’re aware of the techniques and tools available to debug them.

Chapter 7: Fixing the Broken Blocks

125

C07622299.indd 125

C07622299.indd 125

10/24/05 7:14:02 PM

10/24/05 7:14:02 PM

In Summary...

In this chapter, you learned how to use breakpoints; about different techniques to step into, step over, and step out of the source code; and about data visualizers to see the data in the most pertinent way based on its content or context. You also learned how to work with a DLL. You discovered that you can use the Edit and Continue feature to modify variables at run-time and continue the execution.

You learned how you can move the next instruction pointer to re-execute some lines of code. You also started to deal with exceptions and learned the dos and don’ts of debugging. You’ve seen how subtle bugs can find their way in—usually due to distractions and sometimes simply because you don’t possess all of the knowledge and experience—but that’s okay. Don’t worry; you’re in a process called learning.

In the next chapter, you’ll learn about databases, ADO.NET, and manipulating data to and from a Microsoft SQL Server™ Express database. You’ll also learn how to use this data to populate controls on a Windows form. You’ll learn to create an application to add, modify, delete, and visualize rows in the car tracker application.

126

Microsoft Visual C# 2005 Express Edition: Build a Program Now!

C07622299.indd 126

C07622299.indd 126

10/24/05 7:14:03 PM

10/24/05 7:14:03 PM

Chapter 8

Managing

the Data

What Is a Database?, 128

So far, you’ve seen how to build a Windows Forms application and what characteristics those applications contained, but you have not managed a

SQL Server 2005 Express

great deal of data. Managing data is always a concern, whether at home, at

in Visual C# 2005 Express

the office, at school, or even for recreation. For instance, I have many recipes

Edition, 136

and ideas for great dinners, but when I want to prepare a nice meal, it takes

What Are ADO.NET and

me so much time to find them that usually I change my mind. If I had this

Databinding?, 148

information in my computer, it would be easy to quickly access my recipe for C08622299.indd
8

“rack of lamb with herb crust” and prepare a fabulous meal. I could also add other pertinent information to the recipe file, such as what side dishes were served with it or what wines went well with this recipe. I could even add a picture of the finished meal.

You could manage data using a word processing program, such as

Microsoft Word, but it could become unmanageable as soon as you collect a lot of recipes and need to search for information within that file. Using a spreadsheet, such as Microsoft Excel, is also problematic. The fact is that trying to find information quickly when using more than one variable is close to impossible. Using the recipe example, suppose you want to retrieve all of the recipes that can serve at least six people and that have lamb stew meat but no mint in the ingredients because one of your guests is allergic to mint. Imagine the time it would take to find that information in either a Word file or an Excel spreadsheet. That’s where databases come to the rescue.

127

127

C08622299.indd 127

10/24/05 4:02:22 PM

10/24/05 4:02:22 PM

In this chapter, you’ll learn what a database is; how to create a database; how to add, delete, and update data; how to search or query a database; and how to use a database in a Windows Forms application. Accompanying Visual C# 2005 Express Edition is SQL Server 2005 Express Edition, which is a fully workable version of its bigger brother, SQL Server 2005, but with fewer features. SQL Server 2005 Express Edition is free, easy to use, and geared toward building simple and dynamic applications.

What Is a Database?

A database is a collection of data that is stored in files on disks using a systematic structure. The systematic structure enables users to query the data using management software called Database Management System (DBMS). SQL Server 2005 is a Relational Database Management System (RDBMS). It is based on a relational model because its data is structured using sets (the sets theory in mathematics) and logical relations (predicates). Most commercial database products are based on the relational model. In fact, it’s been one of the most popular models for the last 20 years. Apart from Microsoft SQL Server, you may have also heard of the following product names: Oracle or IBM DB2.

What’s In a Database?

N O T E

A relational database, such as SQL Server 2005, contains multiple tables that are related
You’ll learn about some of the

together. A database can also contain views, stored procedures, functions, indexes, security
other elements contained in a

relational database later in this

information, and other elements. In this section, you’ll learn about the basic element of a
chapter.

relational database, which is a table and its components.

A table contains columns and rows. A column defines the type of data, and a row contains the actual data. Because the relational model has strict rules, a RDBMS that uses the relation model must implement them.

M O R E I N F O

In reality, no popular RDBMS is fully implementing the pure relational model
as it was first created in the 1970s.

128

Microsoft Visual C# 2005 Express Edition: Build a Program Now!

C08622299.indd 128

C08622299.indd 128

10/24/05 4:02:28 PM

10/24/05 4:02:28 PM

Data Normalization and Data Integrity

The rules defining the relational model are called normalization rules. Normalization is a process that data architects must apply whenever they are at the design phase. Normalization rules exist to reduce the chance of having the same data stored in more than one table; in other words, they exist to reduce the level of redundancy and also to preserve data integrity in the database. Logically, the normalization process exists to help split the data into its own table so that there is no duplication of information in more than one table. For example, having an application in which the customer’s address, city, state or province, zip or postal code, and country are duplicated in two different tables is a bad idea. There should be only one link from the customer table to the other table referencing additional customer information. Having duplicate data would make updates and deletions more problematic and would also pose the risk of having modified data in one table and not the other. This example demonstrates a data integrity problem.

Let’s look at another data integrity problem. Suppose you have both a product table and a table containing customer order details. Although you normalized your data, data integrity does not exist (for this example). Now let’s say you decide to delete product1, which means removing a row from the product table that corresponds to product1. If the RDBMS would let you do this, it would mean that suddenly all rows in the customer order details table that contained this product would not be able to show which product was ordered because the product would no longer exist. Those rows would be orphaned, which could have disastrous results for the company.

As you can see, data integrity is a very important concept that is related to the accuracy, validity, and correctness of the data. To better understand some of these concepts, let’s look at another example.

Suppose you are the owner of an online store and want to manage your company using a software application. To use a software application, you must start thinking about using a database. Any company, both small or large, typically has a great deal of data to store. Also, because data is all around us, people want more access to this data so as to create reports

N O T E

and conduct analysis. That is why databases are so useful. Returning to your online store, at
The following tables have pur-

posely been kept simple (some

a minimum you would like to store information about your customers, products, invoices,
columns are missing) and are used

purchasing, and inventory. To summarize all of those areas, let’s take a look at the Product,
to illustrate the concepts you’ve

just learned.

OrderHeader, and OrderDetail tables.

Chapter 8: Managing the Data

129

C08622299.indd 129

C08622299.indd 129

10/24/05 4:02:29 PM

10/24/05 4:02:29 PM

Column Name

Data Type

Allow Nulls?

ProductID (PK)

integer

Not Null

ProductNumber

nvarchar(10)

Not Null

Name

nvarchar(50)

Not Null

Description

nvarchar(200)

Null

Photo

image

Null

Price

money

Not Null

Table 8-1

Taxable

bit

Not Null

Product Table

Column Name

Data Type

Allow Nulls?

OrderID (PK)

integer

Not Null

OrderDate

datetime

Not Null

DueDate

datetime

Not Null

CustomerID (FK)

integer

Not Null

TaxAmount

money

Not Null

Table 8-2

Total

money

Not Null

OrderHeader Table

130

Microsoft Visual C# 2005 Express Edition: Build a Program Now!

C08622299.indd 130

C08622299.indd 130

10/24/05 4:02:29 PM

10/24/05 4:02:29 PM

Column Name

Data Type

Allow Nulls?

OrderID (PK) (FK)

integer

Not Null

LineDetailID (PK)

BOOK: Microsoft Visual C# 2005 Express Edition: Build a Program Now!
7.36Mb size Format: txt, pdf, ePub
ads

Other books

After Hours by Swallow, Stephanie
Kiss the Morning Star by Elissa Janine Hoole
Crystal Venom by Steve Wheeler
Relief Map by Rosalie Knecht
Cart Before The Horse by Bernadette Marie
Balance by Kurt Bartling