ADO.net question (access field with it's name).

I

Ivan Sammut

Hi,

I have just started using ADO so this question may look a
bit dump.

I created a query and everything works fine.

ListViewItem iItem = null;
OleDbConnection conn = new OleDbConnection
("Provider=Microsoft.Jet.OleDb.4.0; Data
Source=c:\\program files\\visual dataflex 8.2
\\bin\\pda\\pda.mdb");
OleDbCommand cmd = null;
OleDbDataReader rec1 = null;
conn.Open();
cmd = new OleDbCommand("select * from
pusers",conn);
rec1 = cmd.ExecuteReader();
while (rec1.Read())
{
iItem = new ListViewItem();
iItem.Text = rec1.GetValue(1).ToString
();
listView1.Items.Add(iItem);
}


The problem is this line "iItem.Text = rec1.GetValue
(1).ToString();"

is there a way by which instead of a field index I can use
the field name??


Thanks
Ivan Sammut
 
M

Miha Markic

Hi Ivan,

Before the loop you might retrieve column index by using GetOrdinal(string)
method.
Later you can use it for retrieving data.
And, btw, there is also a GetString method.
 
C

Chris Torgerson

Just as a note the datareader also has an indexer, so just
reference as follows:
iItem.Text = rec1.["fieldname"].ToString();

hth
Chris Torgerson
 

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