locked
Populating a DataSet RRS feed

  • Question

  • When Populating a DataSet is it ok to populate it using DataTable objects..  Example below.  Is this good code?  Also is it ok to use 1 Data Adapter object per table?  I'm going to have about 30 tables in my dataset.

    DataSet dataset = new DataSet();
    
    DataTable table1 = new DataTable("TABLE1");
    DataTable table2 = new DataTable("TABLE2");
    DataTable table3 = new DataTable("TABLE3");
    DataTable table4 = new DataTable("TABLE4");
    DataTable table5 = new DataTable("TABLE5");
    DataTable table6 = new DataTable("TABLE6");
    DataTable table7 = new DataTable("TABLE7");
    DataTable table8 = new DataTable("TABLE8");
    DataTable table9 = new DataTable("TABLE9");
    DataTable table10 = new DataTable("TABLE10");
    
    DataAdapter adapterTable1 = new DataAdapter("SELECT", conn);
    DataAdapter adapterTable2 = new DataAdapter("SELECT", conn);
    DataAdapter adapterTable3 = new DataAdapter("SELECT", conn);
    DataAdapter adapterTable4 = new DataAdapter("SELECT", conn);
    DataAdapter adapterTable5 = new DataAdapter("SELECT", conn);
    DataAdapter adapterTable6 = new DataAdapter("SELECT", conn);
    DataAdapter adapterTable7 = new DataAdapter("SELECT", conn);
    DataAdapter adapterTable8 = new DataAdapter("SELECT", conn);
    DataAdapter adapterTable9 = new DataAdapter("SELECT", conn);
    DataAdapter adapterTable10 = new DataAdapter("SELECT", conn);
    
    dataset.Tables.Add(table1);
    dataset.Tables.Add(table2);
    dataset.Tables.Add(table3);
    dataset.Tables.Add(table4);
    dataset.Tables.Add(table5);
    dataset.Tables.Add(table6);
    dataset.Tables.Add(table7);
    dataset.Tables.Add(table8);
    dataset.Tables.Add(table9);
    dataset.Tables.Add(table10);
      
    }
    
    

     

     

    Sunday, July 25, 2010 4:16 PM

Answers

  • Hello,

    It's OK to populate DataSet using DataTable. And it's OK to use one DataAdapter per table to load the data, but you could only use one actually. Please allow me to assume you use SQL Server as your database, you could try the following code snippet to populate data from one of the 30 tables in your dataset:

    SqlDataAdapter da = new SqlDataAdapter("Select * ....", conn);
    DataSet ds = new DataSet("dsName");
    da.FillSchema(ds, SchemaType.Source, "SrcTableName");
    da.Fill(ds, "SrcTableName");
    
    DataTable tblAuthors;
    tblAuthors = ds.Tables["SrcTableName"];
    
    foreach (DataRow dr in tblAuthors.Rows)
    {
      Console.WriteLine("{0} {1}",
        dr["row1Name"].ToString(),
        dr["row2Name"].ToString());
    }
    

    For a complete example, please refer to this KB article:
    http://support.microsoft.com/kb/314145

    Best regards


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback, please tell us.
    Welcome to the All-In-One Code Framework!
    • Marked as answer by gocards22 Monday, July 26, 2010 3:41 PM
    Monday, July 26, 2010 9:30 AM

All replies

  • Hello,

    It's OK to populate DataSet using DataTable. And it's OK to use one DataAdapter per table to load the data, but you could only use one actually. Please allow me to assume you use SQL Server as your database, you could try the following code snippet to populate data from one of the 30 tables in your dataset:

    SqlDataAdapter da = new SqlDataAdapter("Select * ....", conn);
    DataSet ds = new DataSet("dsName");
    da.FillSchema(ds, SchemaType.Source, "SrcTableName");
    da.Fill(ds, "SrcTableName");
    
    DataTable tblAuthors;
    tblAuthors = ds.Tables["SrcTableName"];
    
    foreach (DataRow dr in tblAuthors.Rows)
    {
      Console.WriteLine("{0} {1}",
        dr["row1Name"].ToString(),
        dr["row2Name"].ToString());
    }
    

    For a complete example, please refer to this KB article:
    http://support.microsoft.com/kb/314145

    Best regards


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback, please tell us.
    Welcome to the All-In-One Code Framework!
    • Marked as answer by gocards22 Monday, July 26, 2010 3:41 PM
    Monday, July 26, 2010 9:30 AM
  • Ok, so I can use one DataAdapter to Fill the tables.. But if I want to perform updates and such, then I need a seperate Data Adapter for each table that will require updates.. Correct?

    Monday, July 26, 2010 5:53 PM