Skip to content Skip to sidebar Skip to footer

SqlConnection Object

The design of creating a SqlConnection objective are so you coulded to do work with an database. Other ADO.NET targets, such as a SqlCommand and a SqlDataAdapter accept an connection object as a parametric quantity. The sequence of cognitive operation* occurring in the lifetime by an SqlConnection are as follows:

1. Instantiate the SqlConnection.
2. Opened the connection.
3. Pass the connection to other ADO.NET objects.
4. Do database operations with the other ADO.NET objects.
5. Close the connection.
We have already assured however to instantiate a SqlConnection. The rest of the steps, opening, passing, applying, and closing are shown in Listing 1.

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace tes
{
class mydata
{
public static string ConnectionString
{
get
{

return @"Data Source=IB-D4E3FF40E053\SQLEXPRESS;Initial Catalog=tgs;Integrated Security=True";

}
}

private static SqlConnection _connection;
public static SqlConnection Connection
{
get
{
if (_connection == null)
_connection = new SqlConnection(ConnectionString);
if (_connection.State == ConnectionState.Closed)
_connection.Open();
return _connection;
}
}
}
}

Post a Comment for "SqlConnection Object"