1

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?

13
  • do you want to get the columns of a table? Commented Mar 21, 2015 at 16:13
  • Yes, leave them in your table. Commented Mar 21, 2015 at 16:14
  • Please, what is ET? Commented Mar 21, 2015 at 16:19
  • 1
    There are several ways: (1) use a Micro-ORM like Dapper to keep the "raw" SQL statement, but turn the results automagically into a List<SomeClass> - then Dapper does much of the mapping of relational data to your .NET class for you; or (2) - use a SqlDataReader rdr = cmd.ExecuteReader(), loop over all rows read by the data reader use while (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) Commented Mar 21, 2015 at 16:40
  • @Sajeetharan: I want to get the selected values, which will be the columns of the select statement. Commented Mar 21, 2015 at 16:49

2 Answers 2

3

First step: define your "DTO" (data transfer object) to hold the values you read from the database:

public class DTO
{
    public string Dist { get; set; }
    public int ID { get; set; }
    public int DID { get; set; }
    public string Temp { get; set; }
    public decimal Hmd { get; set; }
    public decimal ActEvp { get; set; }
}

(I had to guess what datatype those values might be - adapt as needed)

Second step: install "Dapper Dot Net" into your Visual Studio solution (easiest way is via NuGet - or you can get it from here on Github)

Third step: use Dapper to make your database access much simpler:

string command = "......";  // your SQL query, as before

using (SqlConnection con = new SqlConnection("Data Source=MyDB;Initial Catalog=Graphic;Integrated Security=True"))
{
    con.Open();
    IEnumerable<DTO> results = con.Query<DTO>(command);
    con.Close();
}

That's all there is! Now you have a nice proper .NET List<DTO> with the results from the database, and you can start using them.

Sign up to request clarification or add additional context in comments.

Comments

1

Not an answer, but the sql query is better expressed without nesting the second table, like this:

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 DFiles D ON cast(D.ID as int) = I.ID AND D.FileName = '1'
where I.FileName = 's1'
order by CAST(I.Dist as float)

Additionally, it looks like you're using string types for several columns that really should be stored as a number of some kind in the first place... at least the DFiles.ID and IFiles.Dist columns stand out. That's a huge problem with the schema. Stores those values as int/float in the first place.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.