0% found this document useful (0 votes)
36 views

Lesson 02: The Sqlconnection Object: Connection String Parameter Name Description

This document discusses the SqlConnection object in ADO.NET, which represents a connection to a SQL Server database. It describes how to instantiate a SqlConnection using a connection string, which specifies the data source, database name, and security credentials. It also explains the typical sequence of opening the connection, passing it to other objects like SqlCommand, performing database operations, and closing the connection.

Uploaded by

kravik2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Lesson 02: The Sqlconnection Object: Connection String Parameter Name Description

This document discusses the SqlConnection object in ADO.NET, which represents a connection to a SQL Server database. It describes how to instantiate a SqlConnection using a connection string, which specifies the data source, database name, and security credentials. It also explains the typical sequence of opening the connection, passing it to other objects like SqlCommand, performing database operations, and closing the connection.

Uploaded by

kravik2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Lesson 02: The SqlConnection Object

Introduction
Although working with connections is very easy in ADO.NET, you need to understand
connections in order to make the right decisions when coding your data access routines. 
Understand that a connection is a valuable resource.  Sure, if you have a stand-alone
client application that works on a single database one one machine, you probably don't
care about this.  However, think about an enterprise application where hundreds of users
throughout a company are accessing the same database.  Each connection represents
overhead and there can only be a finite amount of them.  To look at a more extreme case,
consider a Web site that is being hit with hundreds of thousands of hits a day. 
Applications that grab connections and don't let them go can have seriously negative
impacts on performance and scalability.

Creating a SqlConnection Object


A SqlConnection is an object, just like any other C# object.  Most of the time, you just
declare and instantiate the SqlConnection all at the same time, as shown below:

SqlConnection conn = new SqlConnection(


    "Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI");

The SqlConnection object instantiated above uses a constructor with a single argument of
type string.  This argument is called a connection string.  table 1 describes common parts
of a connection string.

table 1.  ADO.NET Connection Strings contain certain key/value pairs for
specifying how to make a database connection.  They include the location,
name of the database, and security credentials.

Connection String
Description
Parameter Name
Identifies the server.  Could be local machine,
Data Source
machine domain name, or IP Address.
Initial Catalog Database name.
Set to SSPI to make connection with user's
Integrated Security
Windows login
User ID Name of user configured in SQL Server.
Password Password matching SQL Server User ID.

Integrated Security is secure when you are on a single machine doing


development.  However, you will often want to specify security based on a SQL
Server User ID with permissions set specifically for the application you are
using.  The following shows a connection string, using the User ID and Password
parameters:

SqlConnection conn = new SqlConnection(


"Data Source=DatabaseServer;Initial Catalog=Northwind;User
ID=YourUserID;Password=YourPassword");

Notice how the Data Source is set to DatabaseServer to indicate that you can identify a
database located on a different machine, over a LAN, or over the Internet.  Additionally,
User ID and Password replace the Integrated Security parameter.

Using a SqlConnection
The purpose of creating a SqlConnection object is so you can enable other
ADO.NET code to work with a database.  Other ADO.NET objects, such as a
SqlCommand and a SqlDataAdapter take a connection object as a parameter. 
The sequence of operations occurring in the lifetime of a SqlConnection are as
follows:

1. Instantiate the SqlConnection.


2. Open the connection.
3. Pass the connection to other ADO.NET objects.
4. Perform database operations with the other ADO.NET objects.
5. Close the connection.

We've already seen how to instantiate a SqlConnection.  The rest of the steps,
opening, passing, using, and closing are shown in Listing 1.

Listing 1.  Using a SqlConnection

using System;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Demonstrates how to work with SqlConnection objects
/// </summary>
class SqlConnectionDemo
{
    static void Main()
    {
        // 1. Instantiate the connection
        SqlConnection conn = new SqlConnection(
            "Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI");

        SqlDataReader rdr = null;

        try
        {
            // 2. Open the connection
            conn.Open();

            // 3. Pass the connection to a command object


            SqlCommand cmd = new SqlCommand("select * from Customers", conn);

            //
            // 4. Use the connection
            //

            // get query results


            rdr = cmd.ExecuteReader();

            // print the CustomerID of each record


            while (rdr.Read())
            {
                Console.WriteLine(rdr[0]);
            }
        }
        finally
        {
            // close the reader
            if (rdr != null)
            {
                rdr.Close();
            }

            // 5. Close the connection


            if (conn != null)
            {
                conn.Close();
            }
        }
    }
}

You might also like