I'd like to programatically move through a table

B

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"
 
C

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
 
O

Otis Mukinfus

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();
}
}
}
 
C

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
 
C

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
 
B

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top