Database Reference
In-Depth Information
Figure 15-2. Dataset, data adapter, and data source interaction
The data adapter constructor is overloaded. You can use any of the following to get a new data
adapter. We're using the SQL Server data provider, but the constructors for the other data providers are
analogous.
SqlDataAdapter da = new SqlDataAdapter();
SqlDataAdapter da = new SqlDataAdapter(cmd);
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
SqlDataAdapter da = new SqlDataAdapter(sql, connString);
So, you can create a data adapter in four ways:
You can use its parameterless constructor (assigning SQL and the connection
later).
You can pass its constructor a command (here, cmd is a SqlCommand object).
You can pass a SQL string and a connection.
You can pass a SQL string and a connection string.
You'll see all this working in action shortly. For now, we'll move on and show how to use data tables,
data columns, and data rows. You'll use these in upcoming sections.
A Brief Introduction to Data Tables, Data Columns, and Data Rows
A data table is an instance of the class System.Data.DataTable . It's conceptually analogous to a relational
table. As shown in Figure 15-1, a data table has collections of data rows and data columns. You can
access these nested collections via the Rows and Columns properties of the data table.
A data table can represent a stand-alone independent table, either inside a data set, as you'll see in
this chapter, or as an object created by another method, as you saw in the previous chapter when a data
table was returned by calling the GetSchemaTable method on a data reader.
A data column represents the schema of a column within a data table and can then be used to set or
get column properties. For example, you could use it to set the default value of a column by assigning a
value to the DefaultValue property of the data column.
You obtain the collection of data columns using the data table's Columns property, whose indexer
accepts either a column name or a zero-based index, for example (where dt is a data table):
DataColumn col = dt.Columns["ContactName"];
 
Search WWH ::




Custom Search