Solved

C# Datasets and XML and data question

Posted on 2004-04-03
4
222 Views
Last Modified: 2010-04-15
Hi All,

I have a problem with the new datasets and getting data from them.
The way I unbderstand , once you make a call to the database, the result is returned in a 'DataSet'. The contents however are not easily avaliable like in C++. The only access to data I found is the XML string.

I need to know how can I traverse the dataset and access specific values. I'm looking to make this dataset as much a recordset as I can. So my I have two questions:

1) Is there a way to treat the DataSet as a Recordset?
2) If not are there ny XML /parser classes in C# which I could use to create my own recordset.


Thanks
Peter
0
Comment
Question by:Pete2003
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 250 total points
ID: 10752288
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10752316
You may work with old ADO in .NET. You may work with ADO .NET without using of DataSets. For example, this is function from my exersize which reads database table and fills ListView without DataSet:

        public void FillListBox()
        {
            SqlConnection myConnection = null;
            SqlCommand myCommand;
            SqlDataReader myReader;

            try
            {
                String sQuery = @"SELECT * FROM Employees";
                myConnection = new SqlConnection(connectionString);
                myCommand = new SqlCommand(sQuery, myConnection);
                myConnection.Open();
                myReader = myCommand.ExecuteReader();

                int nID;
                String sFirstName, sLastName;

                while (myReader.Read())
                {
                    nID = myReader.GetInt32(myReader.GetOrdinal(fieldID));
                    sFirstName = myReader.GetString(myReader.GetOrdinal(fieldFirstName));
                    sLastName = myReader.GetString(myReader.GetOrdinal(fieldLastName));

                    // Add item to listview
                    ListViewItem item = new ListViewItem(nID.ToString());
                    item.SubItems.Add(sFirstName);
                    item.SubItems.Add(sLastName);

                    listView.Items.Add(item);
                }

                initSucceeded = true;
            }
            catch ( Exception ex )
            {
                MessageBox.Show(parentForm, ex.Message, errorTitle);
            }
            finally
            {
                if ( myConnection != null )
                    myConnection.Close();
            }
        }

However, if you are using DataSet, accessing it's rows is easy like accessing of array elements. In the followind sample employeesTable is reference to the table from DataSet:

employeesTable = dataSet.Tables[Employees];

        public void FillListView()
        {
            try
            {
                String sID, sFirstName, sLastName;
                DataRowCollection rows = employeesTable.Rows;
                DataRow row;

                // Read all rows in collection
                for ( int i = 0; i < rows.Count; i++ )
                {
                    row = rows[i];

                    // Extract fields from row
                    sID = row[fieldID].ToString();
                    sFirstName = row[fieldFirstName].ToString();
                    sLastName = row[fieldLastName].ToString();

                    // Add item to listview
                    ListViewItem item = new ListViewItem(sID);
                    item.SubItems.Add(sFirstName);
                    item.SubItems.Add(sLastName);

                    listView.Items.Add(item);
                }
            }
            catch ( Exception ex )
            {
                MessageBox.Show(parentForm, ex.Message, errorTitle);
            }
        }

Decide what way is best for you.
0
 

Author Comment

by:Pete2003
ID: 10757153
Txs for the info ...

I do however have a problem as I do not use VB(your 1st post)  or SQL Server ... I'll be using the Oracle classes connecting to an oracle server. As far as I see it Oracle only returns datasets, so therfore I will only be able to work with those.
0
 

Author Comment

by:Pete2003
ID: 10759033
Not to worry I found some dosumentation on oracle and ADO and figured out myself ... txs anyway
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

624 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