Skip to Content
ADO.NET in a Nutshell
book

ADO.NET in a Nutshell

by Matthew MacDonald, Bill Hamilton
April 2003
Beginner to intermediate
620 pages
21h 48m
English
O'Reilly Media, Inc.
Content preview from ADO.NET in a Nutshell

Working with Columns

The schema for a table is defined by the columns in the table and the constraints on those columns. Columns belonging to the DataTable are stored as DataColumn objects in a DataColumnCollection object and are accessed through the Columns property of the DataTable. This section examines some methods and properties of the DataColumnCollection.

There are two methods that can add a column to a table. The Add( ) method optionally takes arguments that specify the name, type, and expression of the column to be added. An existing column can be added by passing a reference to an existing column. If no arguments are passed, the default names Column1, Column2, Column3, and so on are assigned to the new columns. The following examples show how to create columns within the table:

// adding a column using a reference to an existing column

DataColumn col = new DataColumn("MyColumn, typeof(System.Int32));

dt.Columns.Add(col);

 

// adding and creating a column in the same statement

dt.Columns.Add("MyColumn", typeof(System.Int32));

The second method for adding columns is the AddRange( ) method, which allows more than one column stored in a DataColumn array to be added to the table in a single statement, as shown in the following example:

DataTable dt = new DataTable("MyTable"); // create and add two columns to the DataColumn array DataColumn[] dca = new DataColumn[] {new DataColumn("Col1", typeof(System.Int32)), new DataColumn("Col2", typeof(System.Int32))}; // add the columns in ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

C# 7.0 in a Nutshell

C# 7.0 in a Nutshell

Joseph Albahari, Ben Albahari
ADO.NET Cookbook

ADO.NET Cookbook

Bill Hamilton

Publisher Resources

ISBN: 0596003617Errata Page