Home All Groups Group Topic Archive Search About

I'd like to programatically move through a table

Author
31 Dec 2005 11:58 PM
barret bonden
I'd like to programatically move through a table (call it a recordset in
ADO ) and disply fields  in a textbox. I've gotten as far as the code below
(which just loads a listbox)  I assume I need to work with the dataadaptor ,
but have been unable to find info on the web as to how to do it -   the hope
is to be able to work , record by record,; something like the .movenext
capacity in the recordset objects of DAO and ADO.

myConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\t.mdb")

myConnection.Open()

'myCommand = New OleDbCommand("select * from main", myConnection)

Dim myDataAdapter As New OleDb.OleDbDataAdapter( _

"Select * from main", myConnection)

Dim myDataSet As New DataSet()

myDataAdapter.Fill(myDataSet, "main")

'Dim myReader As OleDb.OleDbDataReader

Dim DataViewManager1 As DataViewManager = myDataSet.DefaultViewManager

ComboBox1.DataSource = DataViewManager1

ComboBox1.DisplayMember = "main.First"

Author
2 Jan 2006 12:17 AM
Cyril Gupta
Hello,

Well, your code reads right till the time you open the DataAdapter, but
after it just seems to disintegrate. There's actually no Recordset kind of
object in ADO.Net. I think you need to concentrate on the DataSet, it is
Recordset's alternative in ADO.Net.

You can fill a DataSet using the DataAdapter and then you can iterate
through the Rows object of the DataSet. It is quite like the Recordset, but
the Update operations are significantly different.

Cheers
Cyril
Author
2 Jan 2006 3:18 PM
Otis Mukinfus
On Sat, 31 Dec 2005 18:58:53 -0500, "barret bonden"
<art***@networks-cc.com> wrote:

Show quote
> I'd like to programatically move through a table (call it a recordset in
>ADO ) and disply fields  in a textbox. I've gotten as far as the code below
>(which just loads a listbox)  I assume I need to work with the dataadaptor ,
>but have been unable to find info on the web as to how to do it -   the hope
>is to be able to work , record by record,; something like the .movenext
>capacity in the recordset objects of DAO and ADO.
>
>myConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
>Source=C:\t.mdb")
>
>myConnection.Open()
>
>'myCommand = New OleDbCommand("select * from main", myConnection)
>
>Dim myDataAdapter As New OleDb.OleDbDataAdapter( _
>
>"Select * from main", myConnection)
>
>Dim myDataSet As New DataSet()
>
>myDataAdapter.Fill(myDataSet, "main")
[snip]

To this point you are doing it correctly.

This is as far as you need to go with gathering data from the
database.

What you want to do from here is loop through each row in the "main"
table.

Here is an example of looping through a table in C#:

using System;
using System.Collections.Generic;
using System.Text;
using TestConsole.SimpleLogDataSetTableAdapters;
using System.Data;

namespace TestConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            SimpleLogDataSet ds = new SimpleLogDataSet();
            BandTableAdapter ta = new BandTableAdapter();

            ta.Fill(ds.Band);

            for (int row = 0; row < ds.Band.Rows.Count; row++)
            {
                for (int col = 0; col < ds.Band.Columns.Count; col++)
                {
                    Console.Write("{0}\t", ds.Band.Rows[row][col]);
                }
                Console.Write("\n");
            }
            Console.ReadLine();
        }
    }
}
Author
2 Jan 2006 4:23 PM
Cindy Winegarden
Hi Barret,

Try code like this:

           For Each oRow In myDataSet.Tables(0).Rows()
                '-- Do stuff here
                Console.WriteLine(oRow.Item(0).Tostring())
            Next

--
Cindy Winegarden  MCSD, Microsoft Visual FoxPro MVP
cindy_winegar***@msn.com  www.cindywinegarden.com


Show quote
"barret bonden" <art***@networks-cc.com> wrote in message
news:zVEtf.3479$2j2.1328@fe11.lga...
> I'd like to programatically move through a table .....
Author
2 Jan 2006 11:12 PM
barret bonden
Got it. Many thanks to all - Cindy, I  looked all over the MS sites for
simple and complete examples of how to
*use* the datasets in this way - can you say something to someone at MS
about this ? (I've been trying for years) either I missed it or it's just
not there in this fashion ; I can't lean with out being able to play , and
one can't play without the whole bit of code that makes things work ...
again , thanks -

Dim orow As DataRow
For Each oRow In myDataSet.Tables(0).Rows()
'-- Do stuff here
Console.WriteLine(oRow.Item(0).Tostring())
' ComboBox1.Items.Add(orow.Item(2).ToString())
ComboBox1.Items.Add(orow.Item(2).ToString() + " " + orow.Item(1).ToString())
Next

Show quote
"Cindy Winegarden" <cindy_winegar***@msn.com> wrote in message
news:eVWR3j7DGHA.1312@TK2MSFTNGP09.phx.gbl...
> Hi Barret,
>
> Try code like this:
>
>            For Each oRow In myDataSet.Tables(0).Rows()
>                 '-- Do stuff here
>                 Console.WriteLine(oRow.Item(0).Tostring())
>             Next
>
> --
> Cindy Winegarden  MCSD, Microsoft Visual FoxPro MVP
> cindy_winegar***@msn.com  www.cindywinegarden.com
>
>
> "barret bonden" <art***@networks-cc.com> wrote in message
> news:zVEtf.3479$2j2.1328@fe11.lga...
> > I'd like to programatically move through a table .....
>
>
>
Author
2 Jan 2006 4:49 PM
Cor Ligthert [MVP]
Barret,

An instanced dataset is a disconnected object that holds datatables in a
collection.

In fact is that datatable almost the same as a recordset, with the
difference that the datatable is disconnect.

Therefore
dim dt as datatable = ds.tables(0) 'now I have named ds.tables(0) as well dt
to make typing easy

dt.rows(0) 'is the first row
dt.rows(dt.rows.count-1) 'is the last row
dt.rows(1) 'is the second row

And to see all rows you can use a for index loop or a for each loop as Cindy
shows you.

Does this make it clear?

Cor

AddThis Social Bookmark Button