简体   繁体   English

C# 与 MySQL。 从 MySQL 表中获取有关单元格、行及其值的信息

[英]C# with MySQL. Get information about cells, rows and its values from MySQL table

I'm making application with C# Windows forms with MySQL and I need advices how should I implement the next:我正在使用带有 MySQL 的 C# Windows 表单制作应用程序,我需要建议我应该如何实现下一个:

  1. I need to find the biggest and the largest decimal values from the whole column which could contain any amount of values.我需要从可以包含任意数量值的整列中找到最大和最大的十进制值。 I need to get them to some sort of array or list (?) and then study them or it is better to make it with MySQL commands?我需要将它们放入某种数组或列表(?)然后研究它们,或者最好使用 MySQL 命令制作它?

  2. I need to calculate the sum (or do any other math operation) of all decimal values from MySQL column values.我需要计算 MySQL 列值中所有十进制值的总和(或执行任何其他数学运算)。

And in general where is the best way to do this operations?一般而言,执行此操作的最佳方法是什么? on the MySQL side or on the WinForms side?在 MySQL 端还是在 WinForms 端?

Depending on your business logic and program design, it can be done on either end.根据您的业务逻辑和程序设计,它可以在任何一端完成。 in case of MySql a stored procedure can be created that performs the functionality that you require, and that SP can later be called in your code to produce the results.在 MySql 的情况下,可以创建一个存储过程来执行您需要的功能,并且稍后可以在您的代码中调用该 SP 以生成结果。 if you wish to do that in your winforms app, I'd prefer to use Entity framework to access the data and then use LINQ to perform the functionality that you require.如果您希望在您的 winforms 应用程序中执行此操作,我更愿意使用实体框架来访问数据,然后使用 LINQ 来执行您需要的功能。

You can use the System.Data.MySqlClient namespace to interact with MySQL databases.您可以使用System.Data.MySqlClient命名空间与 MySQL 数据库交互。

Making the calculations on the database side or not is up to you, just know that if you have a large number of records in the table this will take some time.是否在数据库端进行计算取决于您,只要知道如果表中有大量记录,这将需要一些时间。

What I usually do is a simple SELECT and than iterate through the rows on what returns to process the data.我通常做的是一个简单的SELECT然后遍历返回的行来处理数据。 You can use the MySqlDataAdapter to get a DataTable to do so.您可以使用MySqlDataAdapter来获取DataTable来执行此操作。

DataTable dt = new DataTable();

using(var dataAdapter = new MySqlDataAdapter("SELECT decimals FROM myTable", myConnectionString)) {
    dataAdapter.Fill(dt); //fills the datatable
    double allDoubles = 0.0; // stores the sum

DataTable dt = new DataTable();数据表 dt = 新数据表();

using(var dataAdapter = new MySqlDataAdapter("SELECT SUM(decimals) FROM myTable", connectionString)) { dataAdapter.Fill(dt); using(var dataAdapter = new MySqlDataAdapter("SELECT SUM(decimals) FROM myTable", connectionString)) { dataAdapter.Fill(dt); //fills the datatable double myDoubleSum = 0.0; //填充数据表 double myDoubleSum = 0.0; // stores the sum // 存储总和

allDoubles = Convert.ToDouble(row[0].ToString()); 

} } } }

As people said in the comments, you can also do it by using the SUM function which if indexed, will take none to no time at all to complete.正如人们在评论中所说,您也可以通过使用SUM函数来完成,如果索引,则不需要甚至根本不需要时间来完成。

DataTable dt = new DataTable();

using(var dataAdapter = new MySqlDataAdapter("SELECT SUM(decimals) FROM myTable", connectionString)) {
    dataAdapter.Fill(dt); //fills the datatable
    double myDoubleSum = 0.0; // stores the sum

    allDoubles = Convert.ToDouble(row[0].ToString()); 
}

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

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