Sams Teach Yourself Visual Studio .NET 2003 in 21 Days

Sams Teach Yourself .Net in 21 Days

By Jason Beres

Connecting to a Database

To work with any database, the first thing you must do is connect to it. In ADO.NET, you use the Connection object to connect to a database. When accessing SQL Server, the SqlConnection class of the System.Data.SqlClient namespace contains the connection object that you use to connect to a SQL Server database. The OleDbConnection class of the System.Data.OleDb namespace contains the connection object for OLE DB data sources.

When connecting to a database, you must specify the server that the database resides on, the database name, and the authentication information for the database. There are several variations of the connection string, so you might see different syntax based on who wrote the connection string code and the type of authentication used if the database is SQL Server.

The following code gives you an example of each of the following types of connection string options:

vbnet_icon.gif
' Connect to a Microsoft Access database using the
' System.Data.OleDb.OleDbConnection class
Dim strOleDb As String = _
    "Provider=Microsoft.Jet.OLEDB.4.0" _
    & "Data Source=..\Northwind.mdb"
Dim cnOleDb As New OleDbConnection()
cnOleDb.ConnectionString = strOleDb
cnOleDb.Open()

' Connect to the MSDE SDK database using  the
' System.Data.SqlClient.SqlConnection class
' with Integrated Security
Dim strMSDE As String = _
    "Server=(local)\NetSDK;DataBase=Northwind" _
    & "Integrated Security=SSPI"
Dim cnMSDE As New SqlConnection()
cnMSDE.ConnectionString = strMSDE
cnMSDE.Open()

' Connect to a local SQL Server database using  the
' System.Data.SqlClient.SqlConnection class
' with Integrated Security
Dim strSQL As String = _
    "Server=localhost;DataBase=Northwind" & _
    "Integrated Security=SSPI"
Dim cnSQL As New SqlConnection()
cnSQL.ConnectionString = strSQL
cnSQL.Open()

' Connect to a local SQL Server database using  the
' System.Data.SqlClient.SqlConnection class
' passing userid and password in the connection string
Dim strSQL1 As String = _
    "Server=localhost;DataBase=Northwind" & _
    "uid=sa;pwd=password"
Dim cnSQL1 As New SqlConnection()
cnSQL1.ConnectionString = strSQL1
cnSQL1.Open()

c_icon.gif
// Connect to a Microsoft Access database using the
// System.Data.OleDb.OleDbConnection class
string strOleDb;
strOleDb =
   @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\Northwind.mdb";
OleDbConnection cnOleDb = new OleDbConnection();
??? Author added parenthesis after OleDbConnection - Jason
cnOleDb.ConnectionString = strOleDb;
cnOleDb.Open();

// Connect to the MSDE SDK database using  the
// System.Data.SqlClient.SqlConnection class
// with Integrated Security
string strMSDE;
strMSDE =
   @"Server=(local)\NetSDK;DataBase=Northwind;Integrated Security=SSPI";
SqlConnection cnMSDE = new SqlConnection;()
??? Author: added parenthesis after SqlConnection
cnMSDE.ConnectionString = strMSDE;
cnMSDE.Open();

// Connect to a local SQL Server database using  the
// System.Data.SqlClient.SqlConnection class
// with Integrated Security
string strSQL;
strSQL =
   @"Server=localhost;DataBase=Northwind;Integrated Security=SSPI";
sqlConnection cnSQL = new SqlConnection();
??? Author: added parenthesis after SqlConnection
cnSQL.ConnectionString = strSQL;
cnSQL.Open();

// Connect to a local SQL Server database using  the
// System.Data.SqlClient.SqlConnection class
// passing userid and password in the connection string
string strSQL1;
strSQL1 = @"Server=localhost;DataBase=Northwind;uid=sa;pwd=";
SqlConnection cnSQL1 = new SqlConnection();
cnSQL1.ConnectionString = strSQL1;
cnSQL1.Open();

The connection string differences are not that noticeable. You can see the difference between OleDbConnection and SqlConnection is that with SQL Server, you pass the server name (in this case, localhost), the database name, and either the integrated security constant or the actual user ID and password. With the OleDbConnection, you specify the physical path to the database, and most importantly, the OLE DB provider name. Based on the type of OLE DB provider you're using, the connection string might vary. For example, using Microsoft Access, you must specify the physical path to the database. If you're using Oracle or SQL Server 6.5, you must specify a connection string similar to SQL 7.0 or SQL 2000.

Table 10.1 lists the common properties of a SqlConnection object and an OleDbConnection object.

Table 10.1. Properties of the Connection Object

Property

Description

ConnectionTimeout

Gets the time to wait while trying to establish a connection before terminating the attempt and generating an error

Database

Gets the name of the current database or the database to be used after a connection is open

DataSource

Gets the name of the instance of SQL Server to which to connect

PacketSize

Gets the size (in bytes) of network packets used to communicate with an instance of SQL Server

ServerVersion

Gets a string containing the version of the instance of SQL Server to which the client is connected

State

Gets the current state of the connection

WorkstationId

Gets a string that identifies the database client

The SqlConnection and OleDbConnection classes are overloaded, so you can also pass the connection string when you create the connection as the following Visual Basic .NET code demonstrates:

vbnet_icon.gif
Dim cn As New SqlConnection( _
    "Server=localhost;DataBase=Northwind" _
    & "uid=sa;pwd=password")

Now that you can connect to a database, you can start writing some code that retrieves data.

Share ThisShare This

Informit Network