What is the difference between ExecuteQuery and Executenonquery?


ExecuteQuery():

Executes a SQL-Statement on the Database.
ExecuteQuery()------> To Execute SELECT Statement

ExecuteNonQuery():

The ExecuteNonQuery() is one of the most frequently used method in SqlCommand Object, and is used for executing statements that do not return result sets (ie. statements like insert data , update data etc.) .

Command.ExecuteNonQuery();

The ExecuteNonQuery() performs Data Definition tasks as well as Data Manipulation tasks also. The Data Definition tasks like creating Stored Procedures ,Views etc. perform by the ExecuteNonQuery() . Also Data Manipulation tasks like Insert , Update , Delete etc. also perform by the ExecuteNonQuery() of SqlCommand Object.

The following C# example shows how to use the method ExecuteNonQuery() through SqlCommand Object

SqlConnection cnn = new SqlConnection(connetionString);
cnn.Open();
SqlCommand cmd = new SqlCommand("insert into tablename(id,name) values(11,'naresh')", cnn);
cmd.ExecuteNonQuery();

0 comments:

Post a Comment