Articles → ADO.NET → Difference Between ExecuteQuery And ExecuteNonQuery In Ado.Net

Difference Between Executequery And Executenonquery In Ado.Net






Executenonquery






string connectionString = @"[Your_Connection_string]";
string query = "[Your_Query]";
using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();
    using (SqlCommand command = new SqlCommand(query, connection))
    {
         int result = command.ExecuteNonQuery();
    }
}



Executequery




string connectionString = @"[Your_Connection_string]";
string query = "your_select_query";
using (SqlConnection connection = new SqlConnection(connectionString))
{
   connection.Open();
   using (SqlCommand command = new SqlCommand(query, connection))
   {
       SqlDataReader reader = command.ExecuteReader();
       while (reader.Read())
       {
           Console.WriteLine(reader["column_name"]);
       }
   }
}





Posted By  -  Karan Gupta
 
Posted On  -  Wednesday, March 20, 2024

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250