<%@ Page %>
ADO.NET Series - Executing Action Queries
In the previous article of the series we saw how to execute SELECT queries using SqlConnection, SqlCommand and SqlDataReader classes. In this part we will see how to execute action queries like INSERT, UPDATE and DELETE using SqlCommand class.
//set command properties
cmd.CommandText="insert into employees
//fire the query
The code is straight forward. First, we open a connection with the database as usual. Next, we create an instance of SqlCommand object. We set Connection property to the SqlConnection we created before. We also set the CommandText property to required query (INSERT in this case). We then execute the query using ExecuteNonQuery() method of the SqlCommand class. Note that since our query is not returning any results we used ExecuteNonQuery() method and not ExecuteReader() method as in previous article. The ExecuteNonQuery() method returns an
integer the indicated the number of rows affected by the query. We then close the database connection as before.
In our example we used SQL statement as the action query. You can also use stored procedure instead.