Solved

Need a C# DataSet example

Posted on 2011-02-16
20
1,230 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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
 

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 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