There are two primary ways of working with databases in C#with the visual tools in the IDE, and in code. We looked at the visual tools such as the Server Explorer, the Data Adapter Configuration Wizard, the Query Builder, the toolbox, and others, in the previous chapter, and you can handle a great many database tasks using those visual tools. The other way of working with databases in C# is in code, which is the way we'll do things in this chapter. Although the IDE visual tools can do a lot for you, when you really want to drill down to the details, or when you want to do things on the fly at runtime, there's no substitute for doing things yourself in code.
In order to handle databases in code, we have to become familiar with many of the ADO.NET objects, which means wading through a good amount of detail. In this chapter, we're going to work with connection objects, data adapter objects, data table objects, data row objects, dataset objects, data relation objects, and others in code. We're going to see how to connect to a database, how to put SQL into a data adapter, how to create our own database tables from scratch, storing those tables in datasets, and more.
The first task we'll cover is connecting to a data provider, reading data, and displaying that data in a data grid. Doing that in code demands knowledge of connection objects, command objects, and data adapter objects. To retrieve data from a database, you connect to the database with a connection object, assign that connection object to a command object's Connection property, assign the command object to a data adapter object's SelectCommand property, and then use the data adapter's Fill method to fill a dataset. We'll start the chapter by getting acquainted with connection, command, and data adapter objects.