Solved

Need a C# DataSet example

Posted on 2011-02-16
20
1,223 Views
Last Modified: 2013-12-17
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.
0
Comment
Question by:vemi007
20 Comments
 
LVL 9

Expert Comment

by:gdupadhyay
ID: 34911348
0
 

Author Comment

by:vemi007
ID: 34911431
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
 

Author Comment

by:vemi007
ID: 34911498
gdupadhyay: you rlinks contain samples that reference SQL server database. (Oracle.Data.SQLClient) . I am using Oracle database as I have mentioned earlier.
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 34913071
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
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 34913537
0
 

Author Comment

by:vemi007
ID: 34920880
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 34921696
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
 

Author Comment

by:vemi007
ID: 34922011
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 34922857
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:vemi007
ID: 34923184
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 34926664
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
 

Author Comment

by:vemi007
ID: 34927120
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 34927768
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
 

Author Comment

by:vemi007
ID: 34928545
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
 
LVL 96

Accepted Solution

by:
Bob Learned earned 250 total points
ID: 34931460
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
 

Author Comment

by:vemi007
ID: 34935119
like to redo the points now
0
 

Author Closing Comment

by:vemi007
ID: 34935121
I do have healthy respect for every one, else I would not on this site searching for answers.

have a nice day !!!!!!!
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 34939095
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

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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 is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now