Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET - How do I retrieve specific items out of a Dataset?

I have the following code which connects to a database and stores the data into a dataset.

What I need to do now is get a single value from the data set (well actually its two the first row column 4 and 5)

OdbcConnection conn = new OdbcConnection();
    conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString2"].ConnectionString;

    DataSet ds = new DataSet();

    OdbcDataAdapter da = new OdbcDataAdapter("SELECT * FROM MTD_FIGURE_VIEW1", conn);

    da.Fill(ds)

So, I need to get two specific items and store them into ints, the psudo code would be

int var1 = ds.row1.column4
int var2 = ds.row1.column5

Any ideas on how I can do this?

Also, can some one shed a light on data tables too as this may be related to how I'm going about doing this.

like image 761
c11ada Avatar asked Jun 14 '11 15:06

c11ada


People also ask

What is DataSet and DataReader in C#?

DataSet is an in-memory representation of data. Unlike DataReader, it has a disconnected nature, meaning you fill data in DataSet once and then get data from DataSet instead of connecting with the database. A great plus about DataSet is that it can be filled using multiple data sources.

What is a DataSet in VB net?

DataSet is an in-memory representation of data. It is a disconnected, cached set of records that are retrieved from a database. When a connection is established with the database, the data adapter creates a dataset and stores data in it.


1 Answers

You can do like...

If you want to access using ColumnName

Int32 First = Convert.ToInt32(ds.Tables[0].Rows[0]["column4Name"].ToString());
Int32 Second = Convert.ToInt32(ds.Tables[0].Rows[0]["column5Name"].ToString());

OR, if you want to access using Index

Int32 First = Convert.ToInt32(ds.Tables[0].Rows[0][4].ToString());
Int32 Second = Convert.ToInt32(ds.Tables[0].Rows[0][5].ToString());
like image 81
Muhammad Akhtar Avatar answered Oct 05 '22 23:10

Muhammad Akhtar