Wednesday, November 10, 2010

Different ways to create a DataTable and set Schema

Was hanging out StackOverflow and I saw Marc Gravell initializing the DataTable in a simpler and cleaner way. Here goes

DataTable dataTable = new DataTable
    Columns = {
        {"ID", typeof(int)},
        {"Name", typeof(string)},
dataTable.Rows.Add(1, "Naveen", "Coder");

Please note that if you don't specify the type it will automatically be converted to string. See how "Location" is initialised
And my old method

DataTable dtOld = new DataTable("NaveenTest");
dtOld.Columns.Add("MyID", typeof(int));
DataRow drOld = new DataRow();
drOld[0] = 1;
drOld[1] = "Naveen";
drOld[2] = "Coder";


A lot cleaner!
What do you say?

Related Posts :

0 comments on "Different ways to create a DataTable and set Schema"

Add your comment. Please don't spam!
Subscribe in a Reader

Post a Comment