none
Is it possible to fill all (or specified) DataTables in a dataset? (dataset is generated by VS 2013) RRS feed

  • Question

  • My dataset is generated by VS2013.

    I'd like to fill all tables at starting time in the dataset. Is it possible?

    I guess it should be something like:

    foreach(DataTable table in myDataSet.Tables){

    fill(table);

    }

    where the problem is how to implement fill(table). 

    Tuesday, September 9, 2014 5:57 AM

All replies

  • Hi Leon,

    You should provide more information about the tables, its columns etc...

    I am giving you a rough example, check if this is what you wanted and works for you.

    public static void Main(string[] args)
            {               
                //Create a new table - your table will come from the dataset
                DataTable table = new DataTable("Student");
                
                
                // Create a DataColumn and set various properties. 
                DataColumn column_id = new DataColumn(); 
                column_id.DataType = System.Type.GetType("System.Int32"); 
                column_id.AllowDBNull = false; 
                column_id.ColumnName = "ID"; 
        
                
                // Create a DataColumn and set various properties. 
                DataColumn column_name = new DataColumn(); 
                column_name.DataType = System.Type.GetType("System.String"); 
                column_name.AllowDBNull = false; 
                column_name.ColumnName = "Name"; 
                
                //Add the columns to the DataTable
                table.Columns.Add(column_id);
                table.Columns.Add(column_name);
    
                
                // Inserting values for the table
                DataRow row1 = table.NewRow();
                row1["Name"] = "Ram";
                row1["ID"]= 1;
                table.Rows.Add(row1);
                
                
                DataRow row2 = table.NewRow();
                row2["Name"] = "abc";
                row2["ID"]= 2;
                table.Rows.Add(row2);
                
                DataRow row3 = table.NewRow();
                row3["Name"] = "xyz";
                row3["ID"]= 3;
                table.Rows.Add(row3);
                
                //Looping through the table details
                foreach(DataRow dr in table.Rows)
                {
                    foreach(var dc in dr.ItemArray)
                    {
                        Console.WriteLine(dc);
                    }
                }
            }

    The output is :

    1
    Ram
    2
    abc
    3
    xyz
    Hope this helps ! 

    



    Ram.

    Tuesday, September 9, 2014 7:22 AM
  • My dataset is generated by VS2013.

    I'd like to fill all tables at starting time in the dataset. Is it possible?

    I guess it should be something like:

    foreach(DataTable table in myDataSet.Tables){

    fill(table);

    }

    where the problem is how to implement fill(table). 

    That implies that you probably make the same mistake likewise many have done before to use the dataset as a kind of in memory copy of the whole database. 

    The dataset is meant to get the data which is needed with a certain class and then it are mostly not more then four or five tables which have to be filled.

    Be aware there are many samples made likewise you want to do, but that is because it are samples, they mostly don't even io error and concurrency error trapping.


    Success
    Cor


    • Edited by Cor Ligthert Tuesday, September 9, 2014 10:59 AM
    Tuesday, September 9, 2014 10:58 AM
  • Thanks. I think you misunderstood the problem. For "fill", I meant the Fill() method of xxxTableAdapter generated by VS for a database.
    Thursday, September 11, 2014 10:58 AM
  • Thanks. I just want to know how to do this, But Seems there is no mapping between the dataTable and TableAdatper.
    Thursday, September 11, 2014 11:02 AM
  • Thanks. I just want to know how to do this, But Seems there is no mapping between the dataTable and TableAdatper.

    That would in my perception be the same likewise a car and the road. 

    (Although generic the tableadapter is created inside the generic DataSet if it is done by TableAdapterManager, which holds also the references to the datatables).

    http://msdn.microsoft.com/en-us/library/bb384426.aspx


    Success
    Cor

    Thursday, September 11, 2014 12:32 PM