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.
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.
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.
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With