Accessing Oracle query data using WPF / C# / IList interface

735 views Asked by At

I'm rewriting an IronPython WPF app in C# (for assorted reasons). In IPython, I can just do something like:

rows = dt.GetList()

foreach (row in rows);

 ln = row["lastname"]  ... etc.

and so forth. Since C# is a lot more picky about data types, I haven't been able to figure out how to get something like the foreach to work, or even to get the plain indexed version (in the MessageBox below) to work. I'm not to the point where I want to turn it into a CollectionView. Have googled up quite a bit of info on how to do something a lot more complex than this - I may want to do that eventually - but for now, just trying to see if I can get my data back into simple Oracle-like data types - strings, Ints, etc. Suggestions appreciated.

        OracleDataAdapter da = new OracleDataAdapter(sql, db_connection);
        DataTable dt = new DataTable();
        da.Fill(dt);

        System.Collections.IList rows = ((IListSource)dt).GetList();
        MessageBox.Show(rows[0]["lastname"]);
1

There are 1 answers

1
Xavinou On BEST ANSWER

you don't have to convert your datatable, there is already a list of datarow.

With a foreach loop :

foreach (DataRow row in dt.Rows)
    MessageBox.Show(row["id"].ToString());

You can also access with :

MessageBox.Show(dt.Rows[0]["id"].ToString());

To print the whole table :

foreach (DataRow row in dt.Rows)
{
    foreach (DataColumn col in dt.Columns)
    {
        Console.Write(row[col] + " ");
    }
    Console.WriteLine("");
}

With col.DataType, you can retrieve the type ;)

Hope it will help