简体   繁体   English

如何在数组C#winforms中存储SQL列

[英]How to store SQL columns in array C# winforms

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? 有没有更好的办法?

First step : define your "DTO" (data transfer object) to hold the values you read from the database: 第一步 :定义“DTO”(数据传输对象)以保存从数据库中读取的值:

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) 第二步 :在您的Visual Studio解决方案中安装“Dapper Dot Net”(最简单的方法是通过NuGet - 或者您可以从 Github上获取它

Third step : use Dapper to make your database access much simpler: 第三步 :使用Dapper使您的数据库访问更加简单:

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. 现在你有一个很好的.NET List<DTO>和数据库的结果,你可以开始使用它们了。

Not an answer, but the sql query is better expressed without nesting the second table, like this: 不是答案,但sql查询更好地表达而不嵌套第二个表,如下所示:

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. 另外,看起来你正在为几个列使用字符串类型,这些列实际上应该首先存储为某种类型......至少DFiles.IDIFiles.Dist列是突出的。 That's a huge problem with the schema. 这是架构的一个问题。 Stores those values as int / float in the first place. 首先将这些值存储为int / float

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM