The first thing you will need to do when interacting with a data base is to create a connection. The connection tells the rest of the ADO.NET code which data base it is talking to.
A D V E R T I S E M E N T
It manages all of the low level logic associated with the specific data base protocols. This makes it easy for you because the most work you will have to do in code is instantiate the connection object, open the connection, and then close the connection when you are done. Because of the way that other classes in ADO.NET are built, sometimes you don't even have to do that much work.
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 data base 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 data base. 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.
Connection String Parameter Name
Description
Data Source
Identifies the server. Could be local machine, machine domain name,
or IP Address.
Initial Catalog
Data base name.
Integrated Security
Set to SSPI to make connection with user's Windows login
User ID
Name of user configured in SQL Server.
Password
Password matching SQL Server User ID.
Using a SqlConnection
The purpose of creating a SqlConnection object is so you can enable other ADO.NET code to work with a data base. 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:
Instantiate the SqlConnection.
Open the connection.
Pass the connection to other ADO.NET objects.
Perform data base operations with the other ADO.NET objects.
Close the connection.
Example: Using a SqlConnection
using System;
using System.Data;
using System.Data.SqlClient;
class SqlConnectionDemo
{
static void Main()
{
SqlConnection conn = new SqlConnection(
"Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI");
SqlDataReader rdr = null;
try
{
conn.Open();
SqlCommand cmd = new SqlCommand("select * from Customers", conn);
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Console.WriteLine(rdr[0]);
}
}
finally
{
if (rdr != null)
{
rdr.Close();
}
if (conn != null)
{
conn.Close();
}
}
}
}
Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
Keywords:
The SqlConnection Object,asp net sqlconnection,vb net sqlconnection,ado net sqlconnection,asp net object,sqlconnection connection string,vb net object,visual basic net object,visual basic object,c# sqlconnection,sqlconnection net,sqlconnection dataset,using sqlconnection,msdn sqlconnection,sqlconnection string,sqlconnection access,vb sqlconnection,sqlconnection example,c# object,asp object,value object, sqlconnection control