TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# DataColumn Class

This C# tutorial shows DataColumn from the System.Data namespace.

DataColumn designates columns in DataTables.

It specifies the name and type of certain columns in the table. The DataColumn type can be instantiated through the Columns.Add method or through the DataColumn constructor itself.

Constructor

Example. You can construct a DataTable using DataColumn references and the DataColumn constructor. The example also adds some rows to the DataTable based on the types and count of the columns specified by the DataColumn references.

And: The program uses a foreach-loop on the Columns property to print out the names and types of the columns on the DataTable.

C# program that uses DataColumn

using System;
using System.Data;

class Program
{
    static void Main()
    {
	//
	// Construct the DataTable.
	//
	DataTable table = GetTable();
	//
	// Loop over the column headers in the DataTable.
	//
	foreach (DataColumn column in table.Columns)
	{
	    Console.WriteLine("{0} = {1}", column, column.DataType);
	}
    }

    /// <summary>
    /// Generates a DataTable with four columns.
    /// </summary>
    static DataTable GetTable()
    {
	//
	// Here we create a DataTable and add columns to it.
	//
	DataTable table = new DataTable();
	table.Columns.Add("Dosage", typeof(int));
	table.Columns.Add("Medication", typeof(string));
	table.Columns.Add("Patient", typeof(string));
	//
	// Add another column to the data table in a different way.
	//
	DataColumn column = new DataColumn("Appointment", typeof(DateTime));
	table.Columns.Add(column);
	//
	// Here we add some DataRows.
	// Note that the row parameters must match the order and types of the columns.
	//
	table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
	table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
	return table;
    }
}

Output

Dosage = System.Int32
Medication = System.String
Patient = System.String
Appointment = System.DateTime

In this example, we add DataColumns with the Add method on the Columns property on the DataTable instance. Internally, this overload of the Add method actually just constructs a new DataColumn in the same way as shown next.

Overload Method

However: This method signature hides some of the complexity from you, which can help simplify certain programs.

Next, the program shows the DataColumn constructor which receives two parameters. There are other overloaded DataColumn constructors. With them, you can use the same general pattern.

Tip: You can pass an actual DataColumn instance reference to the Add method on the Columns collection.

Finally, we use a foreach-loop after constructing the DataTable. It accesses the Columns property and loops over this with a DataColumn iteration variable. The variable is read-only, but you can access writable data through it.

Then: The loop calls Console.WriteLine. It prints out each column name and the data type the column stores.

Console.WriteLine

Internals. Individual DataColumn references can be accessed by specified by their string names in the indexer on the DataColumnCollection. Internally, these DataColumn references are stored in a Hashtable.

Note: The indexer just uses an internal Hashtable field to look up the references themselves.

IndexerHashtable

Discussion. The DataTable, DataColumn and DataRow classes are in-memory relational database representations in the .NET Framework. The entire point of relational databases such as SQL Server is to store data in a structured way.

However, there is a conflict when translating tabular data to in-memory object models and representations. The DataColumn type is one way you can represent the data in memory without constantly using the database.

Tip: You can find more details on the DataTable class, which involves the DataColumn and DataRow classes mentioned.

DataTable

Summary. We looked at the DataColumn class in the C# language and the .NET Framework, found in the System.Data namespace. The DataColumn acts as a template for the in-memory relational database abstraction specified by the DataTable type.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf