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

Visual CSharp 2005 Express Edition (2006) [eng]

.pdf
Скачиваний:
42
Добавлен:
16.08.2013
Размер:
9.96 Mб
Скачать

Chapter 9

Using the SaveFileDialog Control

The SaveFileDialog control has the same properties as the OpenFileDialog does. In fact, to show you how you can utilize both, I have made the code to use the SaveFileDialog a little more complicated:

string strFileName = openFileDialog1.FileName;

if (strFileName != “”)

{

if (saveFileDialog1.ShowDialog() == DialogResult.OK)

{

strFileName = saveFileDialog1.FileName; richTextBox1.SaveFile(strFileName);

}

}

else

{

richTextBox1.SaveFile(strFileName);

}

This code tests to see if the openFileDialog1 control’s FileName property has been set, through opening a file. If not and the FileName property is equal the name of the dialog box, which is the default, then the SaveFileDialog control is displayed, and the user can specify the file to Save As. If a file has been specified in the OpenFileDialog, then that name is used. This is how the Save option for a new file generally behaves, by only asking for the filename if it hasn’t been saved before.

Try It Out

Adding the SaveFileDialog Control to Your Form

Using the form you have been working with in this chapter:

1.Drag and drop a SaveFileControl from the Dialogs category in the toolbox.

2.Highlight the saveFileDialog1 control with the Properties window open.

3.Double-click the File Save menu item in the menuStrip1 control. The editor opens with the Click event displayed.

4.Type the following lines of code in the body of the routine:

string strFileName = openFileDialog1.FileName;

if (strFileName != “”)

{

if (saveFileDialog1.ShowDialog() == DialogResult.OK)

{

strFileName = saveFileDialog1.FileName; richTextBox1.SaveFile(strFileName);

}

}

else

{

richTextBox1.SaveFile(strFileName);

}

164

Adding Dialog Boxes and Rich Text to Your Application

5.Press F5. The application starts, with the Rich Text Editor you created opened.

You can now test the application by pressing F5 and opening a file already created, or you can edit text opening a new blank file and then choose File Save.

Summar y

The controls you learned how to use in this chapter can be utilized in just about every application you create using C# Express. The RichTextBox control is great when you have an application that you need to edit and work with files other than simple text files. This includes copying information to and from other applications such as Excel or Word documents, with all the formatting intact!

Using the dialog controls, you can do everything from setting fonts to changing text colors. You can open and save files using the OpenFileDialog and SaveFileDialog controls. With the various properties, you can set the dialog boxes to match the task you are trying to accomplish. You will be using the file dialog boxes throughout the rest of the book, starting in the next chapter.

Exercises

1.What is the name of the enumerator used for setting the SelectionAlignment property of the

RichTextBox control?

2.What is one of the methods you can use in code to display all the dialog boxes displayed in this chapter?

3.Which property on the RichTextBox do you use to utilize a font from the FontDialog?

4.What happens if you choose File Open but then click Cancel?

165

Part III

Using Data in

Applications

10

Introducing Database

Concepts

There are all sorts of applications you can create to perform various tasks with C# Express. Some of those programs you create are going to need to work with data. Anytime you need to store data or information, you can use a database. A database is an electronic method for storing data such as customer information, invoices, mailing lists, and more. While there are methods for storing and working with data on the computer other than databases, it makes the most sense to take advantage of the features a database can give you.

This chapter covers data, primarily that used in databases. While you can use data without managing whole databases, having a good understanding of what databases (or in this case relational databases) are and how to work with them will help you create applications that are more logical, powerful, and easy to work with. In this chapter, I give a quick introduction to databases and discuss the following:

What the parts of a database are and how they compare to real-world databases

Kinds of database systems

Relational databases

Getting Started with Databases

As you work with computers you quickly realize that everything you do on a computer deals with data in one sense or another. Whether you are creating a Word document or crunching numbers with Excel, it is all data. However, not all data belongs in a database, and not all programs are meant to be used as a database, although if you look at some people’s documents and worksheets, you may wonder if they are trying to use them as databases. This section explains a few things about databases, as well as shows you how to use real-world databases every day.

Chapter 10

If you have been using Microsoft Office products for a while, you have probably had some experience with or at least heard of databases. In fact, even if you haven’t used databases on the computer, you have used them in real life.

Looking at Databases in the Real World

In the real world, there are a never-ending number of tasks and subjects that work as an example of databases. Every day from the time you get up until the time you go to bed you are dealing with databases of one kind or another. Here are just a few examples of real-world data:

Mailing lists

School registrations

Checking account information and history

Membership lists

Customer information

And the list goes on and on. While some of these items look like simple topics in themselves, undoubtedly additional data for each topic could be flushed out so that more than one topic, what are called tables in database jargon, would be necessary.

The last entry in the list just displayed is a common example of a real-world database, and is worth discussing further. Customer information is stored as business records in manila folders, located in a filing cabinet. In the manila folders customer, information is stored, with either:

One customer’s information stored per folder

All customer information sheets in one folder

Both ways can be analogous to an electronic database and have been used for years in the real world.

In accessing the real-world customer database, you:

1.

2.

3.

4.

5.

6.

7.

Open the file cabinet.

Search through the cabinet for the folder you are looking for.

Pull out the folder.

Look through the folder for the information for which you are searching.

Take the piece, or pieces, of paper containing the information.

Read the data on the page.

Modify the data as necessary.

At this point, you also could add new information by filling out a new form or delete information by throwing away information. (Of course, nowadays you would most likely shred the information for security reasons.)

170

Introducing Database Concepts

It should be noted that the terms below are generic as far as the various database systems are concerned. These terms are discussed in greater details in the next section.

Tables are used to store data in databases. Fields (columns) are used to store individual pieces of data such as customer name, address, and so on. The information supplied in the fields makes up a record (row) in the table. So in this instance, all customer information, such as name, address, city, state, and so on, together make up a record (row). You will find that these terms are used interchangeably when various database products such as Microsoft Access (fields, records) and Microsoft SQL Server (columns, rows) are discussed.

Database Models

Various models of databases exist, two of which are flat-file and relational databases. The relational model of databases is the most common and is used nowadays for desktop and Web development. However, before going deeper into the relational database model, you should know about the flat-file model, including how they store data and their drawbacks.

Flat-File Model Databases

Flat-file model databases store information in single tables, including repeated data. For example, if a store was selling different kinds of coffee and wanted to track customers, invoices, invoice items, and suppliers, the database would look like Figure 10-1, which is a flat-file type table.

Figure 10-1

If you look at this figure closely, you may notice it looks like it was created in Excel, which it was. A lot of new developers and users store data in Excel spreadsheets, thereby creating flat-file tables and databases without realizing it.

There are a number of problems with the flat-file database model. Here are just a few of the issues:

Redundant data. Entries often get repeated, taking up more space than necessary. In Figure 10-1, for example, there is no reason to spell out the names of the suppliers each time.

Error prone. When data has to be repeated, there is more of a chance to enter erroneous data into the table.

Limited columns. Currently, only two products and their information can be entered using the table structure displayed.

Extra work to update. With the redundant data issue, if you want to make any updates, you will have to make sure you parse through the other fields and update those values to match.

In addition to the preceding issues, reporting (retrieving) on the data can be problematic as well. Now take a look at what the relational database model looks like.

171

Chapter 10

Relational Database Model

Unlike the flat-file database model, which stores all data, including related data, in a single record and table, the relational database model use tables that are related to one another to store information. For example, instead of having your coffee invoices all stored in a single table called tblInvoices, the information would be stored in related tables, with customer information being stored in one table, invoices information stored in another, product information in yet another, and so on. Figure 10-2 shows an example of how the flat-file table in the previous section could be structured into a relational model.

Figure 10-2

This figure was taken from C# Express connected to a SQL Server Express database file called CoffeeSQL.mdf, located in the main Samples folder. The figure shows the database diagram of the tables the data is now separated out into, as compared to the single table (spreadsheet) in the previous figure.

C# Express enables you to connect to both Access databases (*.mdb) and SQL Server database files (*.mdf). SQL Server Express is included with C# Express, so the majority of the data-oriented examples and chapters will be using SQL Server Express database files. These are all located in main Samples folder. How to connect to SQL Server Express and Access files is discussed in Chapter 11.

Take a look at some of the benefits of using relational databases. They are pretty much the opposite of the issues found in flat-file databases:

Nonredundant data. Because entries are entered once, and other tables point to the data, there is less redundant data.

Less error prone. When data is entered once in lookup tables, data is then picked from lists. This lends greater control and prevents input errors.

Unlimited data. Because data is stored in rows (down) versus fields (across), the data is not limited to predefined structures. For example, when you want to add another product to an invoice, you simply add another record to tblInvoiceDetails. In the flat file, you would have had to add a third or fourth product column.

Looking at Figure 10-2, you might think maintaining a relational database involves a lot more work because of the multiple tables, but you very quickly learn to appreciate the benefits of the relational database despite the extra work needed in the beginning.

Next, read about the elements that make up relational databases.

Tables: Where Data Is Stored

As mentioned in a note earlier in the chapter, tables are where your data is stored. Tables have specific elements: fields, primary keys, and indexes.

172

Introducing Database Concepts

Columns/Fields

When created, table structures consist of columns (or fields) that represent pieces of data. Columns have properties that give you control over the data that goes into them. Here are a few of those properties common to different database systems such as Access and SQL Server:

Name. Column names are what you will refer to when you want to pull information from the column or assign data to the column. You will want to assign your names to make sense. For example, for the tblCustomer table, the two columns displayed in Figure 10-3 are named CustomerID and CustomerName.

Data types. Data types tell the database system how to handle the data placed in the column. Which data types there are depends on specific database systems. Microsoft Access calls text data under 255 characters text datatype; in SQL Server, it is nvarchar(255).

Other properties. There are a number of other properties that help control data going into the columns, and those properties will again depend on which database system you are using. Some properties, such as Default Value, are used by most systems, but some, such as the Caption property, are used by Access but not SQL Server.

You can see an example of the table structure for tblCustomers listed in Figure 10-3 with the Customer column highlighted. The table structure is displayed in C# Express.

Date Type Property

 

Name Property

 

Primary Key Column

Current Column Highlighted

Figure 10-3

173

Соседние файлы в предмете Программирование на C++