Wednesday, 28 December 2011

Using Microsoft SQL Server as DataUsing Microsoft SQL Server as Database 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