Link to home
Start Free TrialLog in
Avatar of vemi007
vemi007

asked on

Need a C# DataSet example

I am new to using DataSet in C# project. I am building a console application using C#. I have set of data that I need to add/update into an Oracle table. I am using Visual Studio 2008. I followed the following steps..

1. Created a data source and created connection from my project to the database.
2. Created a dataset, opened the dataset designer and dragged a table from server explorer into the designer. This automatically created the DATATABLE and TABLEADAPTER with default methods in Visual Studio 2008.

I am stuck after this point. How do I update my Main() menthod in the console app so that I can interact with the newly created DataTable and TableAdapter to ADD new records and make updates as necessary to the DataSet. I am looking for logic /code sample around this area that will get me started.
Avatar of gdupadhyay
gdupadhyay
Flag of United States of America image

Avatar of vemi007
vemi007

ASKER

The links provided do not match what I am seeing here, the issue I dragged the table into Dataset Designer and it automatially created adapter and data tablle.  The links above are showing how to create a dataset and datatable which in my case are already created. I just need to call them somehow, how to is what I am looking for..
Avatar of vemi007

ASKER

gdupadhyay: you rlinks contain samples that reference SQL server database. (Oracle.Data.SQLClient) . I am using Oracle database as I have mentioned earlier.
Avatar of Dmitry G
Try the following links:

http://www.oracle.com/technology/sample_code/tech/windows/odpnet/DSPopulate/readme.html
http://www.oracle.com/technology/sample_code/tech/windows/odpnet/index.html

and also:

http://www.akadia.com/services/dotnet_dstrans.html

Really, especially on the basic level, all stuff for SQL Server and Oracle is same, just replace SQL classes with Oracle classes, like SqlConnection to OracleConnection etc.
Avatar of vemi007

ASKER

the links provided above give insight into how to instantiate the connectiom, adpater , dataset and then ultimatly fill the dataset. After that the links deviate to a databinding example using Windows Forms which is not my objective. I am ok until filling the adapter after that here is the algorithm below,

1. Once data from "person" table isfilled in to  a dataset.
2. I have already written code to pull person info from an external web service one person at a time. for each person record pulled from the web service, i have to decide as follows
 > first, is the selected person already in the filled dataset, if not add the entire row to dataset
 > second, if exists, then check if there is a difference in field value from the record pulled from web service to person record in the dataset, if there are few field value changes, then update only those field values in the dataset
3. After fetching all records from web service and making corresponding updates to the dataset, finally Update() as per your links above will be exeucted to apply the changes to the table.

I need sample logic around my implementation as explained above ... your info is helpful but looking how to update/add records to dataset as well. PLEASE HELP!!!
I am not sure that I understand what you need, but I am willing to help...it looks like you already have the question answered, since EE questions are meant to be single purpose, and not big, sprawling solutions to every problem.
Avatar of vemi007

ASKER

I am not looking for complete solutions, just the one that willl guide me thru ..some of links provided provide only a small part of what I am looking for. the key part is missing. I am looking for a sample that meets these parts..
1.create Oracle connection, adapter, dataset (already provided in linlk)
2. Fill a dataset (already provided in link)
3. For each person record pulled from the web service, i have to decide as follows
 > Is the selected person already in the filled dataset, if not add the entire row to dataset
 > If exists, then check if there is a difference in field value from the record pulled from web service to person record in the dataset, if there are few field value changes, then update only those field values in the dataset
4.After fetching all records from web service and making corresponding updates to the dataset, finally Update() (already provided in link)

just looking for  example that covers STEP 3 as well so that I can get started on my code.
There are different ways to approach searching for a record in a DataTable:

1) DataTable.Select

2) DataTable.Rows.Find (with a primary key)

3) DataTable.DefaultView.RowFilter

4) Using LINQ with AsEnumerable extension operator (3.5 framework version or higher).

Which one strikes your fancy?
Avatar of vemi007

ASKER

I am operating with an Oracle tables here so LINQ is out of question, I amware of LINQ for Oracle is being developed and it  is not generally available ( beta is in codeplex) I cannot use that . regardign your suggestions how do those fit into my STEP 3 above ? I am looking for a sample..plain vanilla sample / example implementation on how to update fields in a row of data AND to insert new record.
I read this incorrectly,

first, is the selected person already in the filled dataset, if not add the entire row to dataset
 > second, if exists, then check if there is a difference in field value from the record pulled from web service to person record in the dataset, if there are few field value changes, then update only those field values in the dataset

You were referring to this:

After fetching all records from web service and making corresponding updates to the dataset, finally Update() (already provided in link)

So, I am confused what you need help with, since it looks like you already got an answer for #3
Avatar of vemi007

ASKER

Step you are refering to is when ALL changes are made to a dataset then the Update() method is invoked to make the changes back into database via the data adapter.

For Step3 : I am asking for how to update rows in a dataset or add a new record to a dataset  as I loop thru the source data coming from the web service?  this part is not answered yet ..
If I understand you correctly, now, I believe that would be something like this:


using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace My.WeatherService
{

    public class LocalWeatherService
    {

        private string _connectionString = "";
        private DataSet _storeData;
        private SqlDataAdapter _storeAdapter;
        private My.WeatherService.WeatherService.ndfdXMLPortTypeClient _service;

        public LocalWeatherService()
        {
            _connectionString = ConfigurationManager.ConnectionStrings["WeatherStore"].ConnectionString;

            string commandText = "Select * From LocalWeather;Select * From CityState;";

            _storeAdapter = new SqlDataAdapter(commandText, _connectionString);

            _storeData = new DataSet();
            _storeAdapter.Fill(_storeData);
        }

        ~ LocalWeatherService()
        {
            if (_storeAdapter != null && _storeData != null)
            {
                _storeAdapter.Update(_storeData);

                _storeAdapter.Dispose();

                _storeData.Dispose();
            }
        }

        public void SynchronizeWeatherData(string location)
        {
            WeatherResult result = _service.GetWeather(location);

            DataRow dr = _storeData.Tables[0].NewRow();

            dr["City"] = result.City;
            dr["State"] = result.State;
            dr["CurrentTemperature"] = result.CurrentTemperature;

            _storeData.Tables[0].Rows.Add(dr);
        }
    }

}

Open in new window

Avatar of vemi007

ASKER

TheLearnedOne:

We are on the right track now .. thanks much. I have a Q .. I need to check if the employee already exists before I do the "DataRow dr = _storeData.Tables[0].NewRow();" , how do i check for it .. for example lets say Table[0] is "LocatWeather"  and the primary key on the table is 'Weather ID" .. how do i go about  checking if the record already exists before adding it to dataset  in SynchronizeWeatherData() method ?
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vemi007

ASKER

like to redo the points now
Avatar of vemi007

ASKER

I do have healthy respect for every one, else I would not on this site searching for answers.

have a nice day !!!!!!!
My friend, I was just having a bad (production issues), so don't worry about me, I was just a little sensitive that day, and reading into things a bit too much!!