Solved

CSharp - Best collection type to use for updating

Posted on 2011-09-20
4
150 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…

839 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