Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

.Net Technologies Notes, Study notes of Computer Science

What ADO.NET DataTable is and how it is used in C#. The DataTable is a central object that represents relational data in tabular form and is stored in memory. how to create an instance of DataTable, define its schema, add rows to it, and iterate through its rows and columns. It also explains how to create primary key columns and DataRow objects. useful for students learning C# and database programming.

Typology: Study notes

2021/2022

Available from 03/26/2023

srushti-dewalekar
srushti-dewalekar 🇮🇳

5 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
What is ADO.NET DataTable in C#?
The DataTable in C# is similar to the Tables in SQL. That means the DataTable is also going to represent
the relational data in tabular form i.e. rows and columns and this data is going to be stored in memory.
When we create an instance of DataTable, by default, it does not have table schema i.e. it does not have
any columns or constraints by default. You can create the table schema by adding columns and
constraints to the table. Once you define the schema (i.e. columns and constraints) for the DataTable,
then only you can add rows to the data table. In order to use DataTable, you must have to include the
System.Data namespace.
Note: The ADO.NET DataTable is a central object which can be used independently or can be used by
other objects such as DataSet and the DataView.
Step1: Creating DataTable instance
create an empty data table for which the TableName property is set to Student.
Step2: Adding DataColumn and Defining Schema
The following image shows adding Data Column using a few properties.
Creating Primary Key Column in Datatable:
Like SQL, the primary key of a DataTable object also consists of a column or columns that make up a
unique identity for each data row. The following image shows how to set the PrimaryKey property on
the Id column of the Student DataTable object.
pf3
pf4
pf5

Partial preview of the text

Download .Net Technologies Notes and more Study notes Computer Science in PDF only on Docsity!

What is ADO.NET DataTable in C#?

The DataTable in C# is similar to the Tables in SQL. That means the DataTable is also going to represent

the relational data in tabular form i.e. rows and columns and this data is going to be stored in memory.

When we create an instance of DataTable, by default, it does not have table schema i.e. it does not have

any columns or constraints by default. You can create the table schema by adding columns and

constraints to the table. Once you define the schema (i.e. columns and constraints) for the DataTable,

then only you can add rows to the data table. In order to use DataTable, you must have to include the

System.Data namespace.

Note: The ADO.NET DataTable is a central object which can be used independently or can be used by

other objects such as DataSet and the DataView.

Step1: Creating DataTable instance

create an empty data table for which the TableName property is set to Student.

Step2: Adding DataColumn and Defining Schema

The following image shows adding Data Column using a few properties.

Creating Primary Key Column in Datatable:

Like SQL, the primary key of a DataTable object also consists of a column or columns that make up a

unique identity for each data row. The following image shows how to set the PrimaryKey property on

the Id column of the Student DataTable object.

Creating DataRow Objects in C#:

Once you created the DataColumns for the DataTable object, then you can populate the DataTable

object by adding DataRow objects. You need to use the DataRow object and its properties and methods

to retrieve, insert, update and delete the values in the DataTable. The DataRowCollection represents the

actual DataRow objects in the DataTable and it has an Add method that accepts a DataRow object. The

Add method is also overloaded to accept an array of objects instead of a DataRow object. The following

image shows how to create and add data to the Student DataTable object.

You can also add a new DataRow by simply adding the values as shown in the below image.

Iterating the DataTable in C#:

You can use a for each loop to loop through the rows and columns of a data table. The following image

shows how to enumerate through the rows and columns of a data table.

Iterating the DataTable in C#

ADO.NET DataTable Example in C#

using System;

dataTable.Columns.Add(Email); //Setting the Primary Key dataTable.PrimaryKey = new DataColumn[] { Id }; //Add New DataRow by creating the DataRow object DataRow row1 = dataTable.NewRow(); row1["Id"] = 101; row1["Name"] = "Anurag"; row1["Email"] = "Anurag@dotnettutorials.net"; dataTable.Rows.Add(row1); //Adding new DataRow by simply adding the values dataTable.Rows.Add(102, "Mohanty", "Mohanty@dotnettutorials.net"); foreach (DataRow row in dataTable.Rows) { Console.WriteLine(row["Id"] + ", " + row["Name"] + ", " + row["Email"]); } } catch (Exception e) { Console.WriteLine("OOPs, something went wrong.\n" + e); } Console.ReadKey(); } } }