Solved

CSharp - Best collection type to use for updating

Posted on 2011-09-20
4
149 Views
Last Modified: 2012-06-27
Hi

I need some advice on the best way to achieve something

I need to do a SQL Query, that will return some information: Item, Company, Stock

I then need to run another SQL Query, that will have some more information, Demand, Availability. I then want this to be updated, alongside the first lot of information

I don't really want to create a temporary table or do anything within the database, I am trying to find an "offline" way of handling it - but what is the best way of doing that ?

Once that is done, I then want to be able to cycle through it, and if one value is greater than another, for an action to take place

I hope that this makes sense

Thanks
0
Comment
Question by:seancurt
  • 2
4 Comments
 
LVL 18

Expert Comment

by:lludden
ID: 36567122
Dot Net DataTables and Datasets are specifically made for this type of application.
0
 

Author Comment

by:seancurt
ID: 36567196
I did try a DataTable, and everything went in ok. Then trying to do the update, I believe the filter needs to be done on the DataSet, but it seems that the DataTable's dataset is null. Is this beacause I am doing dt.rows.add(row), rather than creating a dataset then putting it into the table - if that makes any sense???


DataSet ds = dt.DataSet;
DataRow[] row = ds.Tables["DataSet"].Select("Item = 'ABC' AND Company = 'DEF'");
0
 
LVL 18

Accepted Solution

by:
lludden earned 500 total points
ID: 36567675
This might be a good time to use a strongly typed dataset.

Create the dataset and fill it.
Make modifications to it.
Call the Update method on the tableadapter to persist your changes.  You need to have the rows be part of a dataset.

0
 
LVL 9

Expert Comment

by:rinfo
ID: 36573717
I think you are confusing about dataset and datatable.
Dataset are  in -memory copy of  a database.
And datatable are table that constitute the database / dataset.
A dataset obiviously could have a number of table/datatable.
For your job you need to define 2 datatable for the dataset. - one for each query.
And these datatable would be populated as follows.
SqlConnection mySqlConnection =new SqlConnection(Your connection string);
 SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
 SqlCommand.CommandText =your query to fetch data from table1";
 SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
SqlDataAdapter.SelectCommand = mySqlCommand;
 DataSet myDataSet = new DataSet();
 mySqlConnection.Open();
  int numberOfRows = mySqlDataAdapter.Fill(myDataSet, "stocklist");//denote any name for emplouee
    Console.WriteLine("numberOfRows = " + numberOfRows);
    SqlDataAdapter.SelectCommand.CommandText =
     yourquery to fetch data from second query.
    numberOfRows = mySqlDataAdapter.Fill(myDataSet, "BOStocklist");//
    Console.WriteLine("numberOfRows = " + numberOfRows);  SqlConnection.Close();

    foreach (DataTable myDataTable in myDataSet.Tables) {
      Console.WriteLine("\nReading from the " +
        myDataTable.TableName + "DataTable");
      foreach (DataRow myDataRow in myDataTable.Rows)
      {
        foreach (DataColumn myDataColumn in myDataTable.Columns)
        {
          Console.WriteLine(myDataColumn + "= " +
            myDataRow[myDataColumn]);
        }
      }
    }
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question