CS 112D Lecture Notes - Data Providers (Chapter 4 part 1)
Namespaces
namespace - a logically related set of classes, functions, subroutines, other namespaces,
etc. When specifying a class that's part of a namespace, the namespace must be specified as well, either
using an "Imports <namespace>" statement before the class is used, or fully qualifying the class
name with <namespace>.class. For example, the SqlConnection class is part of the SqlClient
namespace, which is part of the Data namespace, which is part of the System namespace. So there are two
wasy to declare a SqlConnection object called "myConnection":
Dim myConnection As System.Data.SqlClient.SqlConnection
or:
' This line should be at the top of your code file:
Imports System.Data.SqlClient
' This line can be anywhere later in the file:
Dim myConnection As SqlConnection
Overview of ADO.NET
- ADO.NET is the newest data access model for VB.
- ADO.NET works with 3 different Data Providers, each of which has its own namespace:
- SqlClient - works with SQL Server 7.0 or higher. Namespace is System.Data.SqlClient
- Oledb - works with any database that has an OLEDB provider (the connection format from
earlier versions of VB). Namespace is System.Data.OleDb
- Odbc - works with any database that has an ODBC driver. Namespace is Microsoft.Data.Odbc or
System.Data.Odbc, depending on which version of Visual Studio you have.
- Remember that a class is a data type for an object variable. Unfortunately, your
textbook uses the word "object" to mean class, such as when describing the following:
- Each of the ADO.NET Data Providers has 4 core classes of the following kinds:
- Connection - Establishes a connection to a specific data source
- Command - Executes a command at a data source, such as calling a stored procedure or
executing a query that modifies or returns data
- DataReader - Reads and returns a forward-only, read-only stream of data from a data source
- DataAdapter - Used with a DataSet to retreive and save data via a Connection
- The 4 core classes listed above are optimized for each Data Provider, so there is a different name
for the SqlClient Connection class than the Oledb Connection class. The names for these classes use the
prefixes Sql, OleDb and Odbc. So for example SqlConnection is the class that
establishes a connection to a SQL Server 7.0 or higher database.
- We will study the DataSet class in Chapter 5, but it is Data Provider - independent. So
there is just one DataSet class, used for all Data Providers.
The Connection Classes
- ConnectionString - property to set the connection information. It has many parameters that
can be set. Default values are used for unspecified parameters, e.g.
"data source=L-BAT361-CPERSI\VSDOTNET;initial catalog=novelty"
The ConnectionString can be specified as the constructor's argument.
- State - property allowing us to see if the connection is open, closed, etc. (value will be
System.Data.ConnectionState.xxx). Its value can be converted to a String using its ToString() method.
(parenthesis are missing on page 165, making ToString look like a property.
- Open() - method used to open the connection
Return to the main CS 112D page