Configuring Connections and Connecting to Data in Microsoft .NET Framework 3.5

  • 2/25/2009
This chapter from MCTS Self-Paced Training Kit (Exam 70-505): Microsoft .NET Framework 3.5 Windows Forms Application Development describes how to create the various connection objects that are used to connect applications to different data sources and sets the basis for working with data in the following chapters.

Typically, most real-world applications use databases as a store for the data in that application. For example, inventory systems, contact management systems, and airline reservation systems store data in a database and then retrieve the necessary records into the application as needed. In other words, the data that an application uses is stored in a database external to the actual application, and it is retrieved into the application as required by the program.

When creating applications that work with data, the Microsoft .NET Framework provides many classes that aid in the process. The classes that you use for common data tasks, such as communicating, storing, fetching, and updating data, are all located in the System. Data namespace. The classes in the System.Data namespace make up the core data access objects in the .NET Framework. These data access classes are collectively known as ADO.NET.

Before you can begin working with data in an application, you must first establish and open a connection to communicate with the desired data source. This chapter describes how to create the various connection objects that are used to connect applications to different data sources and sets the basis for working with data in the following chapters. After learning to establish connections to databases in this chapter, we will move on to Chapter 6, which provides instructions for running queries, saving data, and creating database objects directly between your application and a database. Chapter 7, describes how to create DataSet and DataTable objects that allow you to temporarily store data while the data is being used in a running application. Finally, Chapter 8, provides information on binding data to be displayed and worked with in Windows Forms controls.

Typically, data sources are relational databases like Microsoft SQL Server and Oracle, but you can also connect to data in files such as Microsoft Office Access (.mdb) and SQL Server (.mdf) database files. The connection object you use is based on the type of data source your application needs to communicate with.

Exam objectives in this chapter:

  • Manage connections and transactions.

    • Configure a connection to a database using the Data Source Configuration Wizard.

    • Configure a connection to a database using Server Explorer.

    • Configure a connection to a database using the Connection class.

    • Connect to a database using specific database connection objects.

    • Enumerate instances of SQL Server.

    • Open an ADO.NET connection to a database.

    • Close an ADO.NET connection to a database by using the Close method of the connection object.

    • Protect access to the connection details of a data source.

    • Create a connection designed for reuse in a connection pool.

    • Control a connection pool by configuring ConnectionString values based on database type.

    • Use the Connection events to detect database information.

    • Handle exceptions when connecting to a database.

Lessons in this chapter:

  • Creating and Configuring Connection Objects

  • Connecting to Data Using Connection Objects

  • Working with Connection Pools

  • Handling Connection Errors

  • Enumerating the Available SQL Servers on a Network

  • Securing Sensitive Connection String Data

Before You Begin

To complete the lessons in this chapter, you must have:

  • A computer that meets or exceeds the minimum hardware requirements listed in the “Introduction” at the beginning of the book.

  • Microsoft Visual Studio 2008 Professional Edition installed on your computer.

  • An understanding of Microsoft Visual Basic or C# syntax and familiarity with the .NET Framework.

  • A basic understanding of relational databases.

  • Available data sources, including SQL Server 2005 or later (SQL Server 2005 Express Edition or later is acceptable), the Northwind sample database for SQL Server, and the Nwind.mdb Access database file. Directions for setting up the sample databases are located in the Setting up Sample Databases Read Me file on the companion CD.