Wednesday 8 February 2012

MICROSOFT SQL SERVER AS DATABASE USE IN ASP.NET


Establishment Of Connection To Sql Server :


SqlConnection connection = new SqlConnection();

SqlCommand command = new SqlCommand();

if (connection .State == ConnectionState.Closed)
{
connection .ConnectionString = "CONNECTION STRING HERE";

connection .Open();
}
command .CommandText = "SQL QUERY HERE";

command .Connection = connection ;

//PERFORM THE OPERATIONS HERE ACCORDINGLY

connection .Close();

USING SqlDataReader :

SqlConnection connection = new SqlConnection();

SqlCommand command = new SqlCommand();

if (connection .State == ConnectionState.Closed)
{
connection .ConnectionString = "CONNECTION STRING HERE";

connection .Open();
}
command .CommandText = "SQL QUERY HERE";

command .Connection = connection ;

SqlDataReader datareader = command .ExecuteReader();

connection .Close();

USING DataAdapter :

DataSet obj = new DataSet();

SqlConnection connection = new SqlConnection();

SqlCommand command = new SqlCommand();

SqlDataAdapter adapter = new SqlDataAdapter();

if (connection .State == ConnectionState.Closed)
{
connection .ConnectionString = "CONNECTION STRING HERE";
connection .Open();
}
command .CommandText = "SQL QUERY HERE";

command .Connection = connection ;

adapter .SelectCommand = command ;

adapter .Fill(obj);

connection .Close();

 

No comments:

Post a Comment