Click here to Skip to main content
15,891,745 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai,


I am having doubts regarding datatable.Suppose we declare a datatable with some name.For ex:
datatable dt=new datatable();


dt is the name of datatable

At the same time we write it as
datatable dt=new datatable("customers");


In the above which is the name of datatable whether dt (or) customers
Posted

You are confusing a table name with an object name.

DataTable dt = new DataTable ();

In this, dt is an object or instance of the class DataTable.

DataTable dt = new DataTable ("customers");

In this, dt is an object or instance of the class DataTable and customers is the table name being passed as a parameter to the constructor.
 
Share this answer
 
v3
dt is the name of your variable. In the first example your DataTable does not have a name.
So imagine that you would add your DataTable to a DataSet. In the first example you could only access the DataTable in the DataSet by its index, since you have not provided a name for the actual DataTable. You only have a variable name which lets you reference it.
In the Second example you have named your DataTable "customers". If you would add this to a DataSet you could access the DataTable in the DataSet by asking for the "customers" DataTable, rather than its index.

So for examples:
C#
System.Data.DataSet ds = new System.Data.DataSet();
// First example.
System.Data.DataTable dt1 = new System.Data.DataTable();
ds.Tables.Add(dt1);
ds.Tables[0].Clear();

// Second example.
System.Data.DataTable dt2 = new System.Data.DataTable("customers");
ds.Tables.Add(dt2);
ds.Tables["customers"].Clear();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900