Databases Reference
In-Depth Information
3. Make OrdinalIndexer the startup project, and run it by pressing Ctrl+F5. You
should see the results in Figure 12-2.
Figure 12-2. Displaying multiple columns
How It Works
You query the Customers table for the columns CompanyName and ContactName,
where contact names begin with the letter “M.”
// query
string sql = @"
select
companyname,
contactname
from
customers
where
contactname like 'M%'
";
Since two columns are selected by your query, the returned data also comprises a
collection of rows from only these two columns, thus allowing access to only two possible
ordinal indexers, 0 and 1.
You read each row in a while loop, fetching values of the two columns with their
indexers. Since the returned value is an object, you need to explicitly convert the value to
a string so that you can use the PadLeft method to format the output in such a way that
all the characters will be right-aligned, being padded with spaces on the left for a speci-
fied total length.
Search WWH ::




Custom Search