Using SqlCommand Objects in Code

SqlCommand objects let you execute many different types of queries. Some SqlCommand objects retrieve data in the form of a resultset, and others modify the content or structure of the data store. Let’s look at how to create SqlCommand objects and use them to perform a variety of tasks.

Creating a SqlCommand Object

You can create a SqlCommand object in three ways. The first way is simply to create an instance of an object using the new keyword and then set the appropriate properties. Second, you can use one of the available constructors to specify the query string and a SqlConnection object.

The third way is to call the CreateCommand method of the SqlConnection class (which we examined in Chapter 3). The three approaches ...

Get Programming Microsoft® ADO.NET 2.0 Core Reference, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.