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.
vemi007Asked:
Who is Participating?
 
Bob LearnedConnect With a Mentor Commented:
Aaah, no respect for busy people who donate their precious time for free (d'oh).

Use DataTable.Rows.Find.

How to: Locate a Specific Row in a DataTable
http://msdn.microsoft.com/en-us/library/y06xa2h1(v=vs.80).aspx
0
 
gdupadhyayCommented:
0
 
vemi007Author Commented:
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..
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
vemi007Author Commented:
gdupadhyay: you rlinks contain samples that reference SQL server database. (Oracle.Data.SQLClient) . I am using Oracle database as I have mentioned earlier.
0
 
anarki_jimbelCommented:
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.
0
 
SAMIR BHOGAYTAFreelancer and IT ConsultantCommented:
0
 
vemi007Author Commented:
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!!!
0
 
Bob LearnedCommented:
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.
0
 
vemi007Author Commented:
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.
0
 
Bob LearnedCommented:
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?
0
 
vemi007Author Commented:
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.
0
 
Bob LearnedCommented:
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
0
 
vemi007Author Commented:
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 ..
0
 
Bob LearnedCommented:
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

0
 
vemi007Author Commented:
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 ?
0
 
vemi007Author Commented:
like to redo the points now
0
 
vemi007Author Commented:
I do have healthy respect for every one, else I would not on this site searching for answers.

have a nice day !!!!!!!
0
 
Bob LearnedCommented:
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!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.