C# 查询SQlite 的数据表如何计算其中每列的和
时间: 2024-03-21 14:43:06 浏览: 19
你可以使用SQL语句中的SUM函数来计算每列的和。下面是一个示例代码,假设你的表名为"table_name",需要计算的列为"column_name":
```csharp
using System.Data.SQLite;
// 连接到数据库
string connectionString = "Data Source=your_database_file_path;";
SQLiteConnection connection = new SQLiteConnection(connectionString);
connection.Open();
// 执行查询
string query = "SELECT SUM(column_name) FROM table_name;";
SQLiteCommand command = new SQLiteCommand(query, connection);
object result = command.ExecuteScalar();
// 处理结果
if (result != null && result != DBNull.Value)
{
double sum = Convert.ToDouble(result);
Console.WriteLine("Sum of column_name: " + sum);
}
// 关闭连接
connection.Close();
```
在上面的代码中,我们使用了SQLiteCommand对象的ExecuteScalar方法来执行查询并返回结果。如果查询返回了非空结果,我们将其转换为double类型并输出。