Connecting to a Database
397
PICKING A DATABASE
Picking the right database product is a tough decision. Access databases have the
advantage that a C# program can read and manipulate one even if Access isn’t
installed. That means you can build a database on one computer that has Access
installed and then copy it to another computer without Access and use it there.
SQL Server, Oracle, MySQL, and similar database products tend to provide more
database features than Access. For example, they support bigger databases, triggers,
views, and other features that Access does not provide.
A common compromise is to start development with SQL Server Express, a free
version of SQL Server that has some size restrictions. Later if you decide you need
the extra space provided by the full version of SQL Server, you can upgrade rela-
tively easily. You can learn more about SQL Server Express and download it at
www.microsoft.com/express/Database.
Unfortunately a C# program cannot use these more powerful databases unless you
have them installed, an assumption I don’t want to make, so this Lesson works
with Access databases. You can get the necessary databases from the lesson’s code
download and use them even if you don’t have Access installed.
If you’re planning to do more database programming, I encourage you to install
one of the more powerful database products, particularly since SQL Server Express
and MySQL are free.
4. When you click Next, Visual Studio asks whether you want to include the database in the
project. Click Yes to copy the database file into the project so it can easily be distributed with
the program.
5. The dialog’s next page asks whether you want to include the database connection string in
the program’s configuration file so the program can use that string to connect to the database
at run time. This is often convenient because it lets you change the connection string without
rebuilding the application. Note, however, that you shouldn’t store database passwords in
the configuration file, so if the database requires a password, you may want to leave the con-
nection string out of the configuration file.
When you click Next again, you see the page shown in Figure 35-4.
6. Expand the database object treeview and select the tables and fields that you want the pro-
gram to use. In this example, the database contains only one table. In Figure 35-4 I selected
the Tables entry and that selected the table and all of its fields.
7. When you click Finish, Visual Studio defines a dataset that can hold the data in the database.
It also adds some code to make working with the dataset easier.
Now that you’ve added a data source to the project, Visual Studio provides easy ways to make two
kinds of database programs: one that displays data in a grid and one that displays data one record
at a time.
596906c35.indd 397 4/7/10 12:34:51 PM