I want to store my selected values from database in arrays so I can do computations on the values. I wrote the following code:
string command = @"select
I.Dist, I.ID, D.ID, D.Temp,
CAST(D.Hmd as float) Hmd,
CAST(D.ActEvp as float) ActEvp
from
IFiles I
inner join
(select
DFiles.ID, DFiles.Temp, DFiles.Hmd, DFiles.ActEvp
from
DFiles
where
FileName = '1') D on I.ID = CAST( D.ID as int)
where
FileName = 's1'
order by
CAST(I.Dist as float)";
SqlConnection con = new SqlConnection("Data Source=MyDB;Initial Catalog=Graphic;Integrated Security=True");
SqlCommand cmd = new SqlCommand(command, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
con.Open();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
da.Fill(ds, "DFiles");
List<float> Hmd = new List<float>();
foreach(DataRow row in ds.Tables["DFiles"].Rows)
{
Hmd.Add(float.Parse(row["Hmd"].ToString()));
}
However I don't think it will be efficient if I do this for every single column. Is there a better way?
ET?List<SomeClass>- then Dapper does much of the mapping of relational data to your .NET class for you; or (2) - use aSqlDataReader rdr = cmd.ExecuteReader(), loop over all rows read by the data reader usewhile (rdr.Read()) { ... }and inside the while loop create objects and fill their properties with the values read from the database (that's basically what Dapper does for you - if you insist, you can code it all yourself)