报错了:string query1 = "SELECT 季度,COUNT(CASE WHEN 季度 = 'Q1' THEN 1 ELSE NULL END) AS Q1Count, COUNT(CASE WHEN 季度 = 'Q2' THEN 1 ELSE NULL END) AS Q2Count,COUNT(CASE WHEN 季度 = 'Q3' THEN 1 ELSE NULL END) AS Q3Count,COUNT(CASE WHEN 季度 = 'Q4' THEN 1 ELSE NULL END) AS Q4Count FROM FAIsum WHERE 审核年份 = @value3 GROUP BY 季度"; adapter.SelectCommand.Parameters.AddWithValue("@value3", dateTimePicker1.Text); SQLiteDataAdapter Myadapter1 = new SQLiteDataAdapter(query1, con); DataTable dtNEW = new DataTable(); Myadapter1.Fill(dtNEW); dataGridView1.DataSource = dtNEW; con.Close();
时间: 2024-01-24 14:01:41 浏览: 107
这个错误通常是由于在执行命令之前没有打开数据库连接所导致的。请检查你的代码,确保在执行查询之前先打开了数据库连接。具体的代码可能类似于:
```csharp
using (var connection = new SQLiteConnection(connectionString))
{
connection.Open();
string query1 = "SELECT 季度,COUNT(CASE WHEN 季度 = 'Q1' THEN 1 ELSE NULL END) AS Q1Count, COUNT(CASE WHEN 季度 = 'Q2' THEN 1 ELSE NULL END) AS Q2Count,COUNT(CASE WHEN 季度 = 'Q3' THEN 1 ELSE NULL END) AS Q3Count,COUNT(CASE WHEN 季度 = 'Q4' THEN 1 ELSE NULL END) AS Q4Count FROM FAIsum WHERE 审核年份 = @value3 GROUP BY 季度";
using (var command = new SQLiteCommand(query1, connection))
{
command.Parameters.AddWithValue("@value3", dateTimePicker1.Text);
using (var adapter = new SQLiteDataAdapter(command))
{
DataTable dtNEW = new DataTable();
adapter.Fill(dtNEW);
dataGridView1.DataSource = dtNEW;
}
}
}
```
请注意,在这个例子中,我们使用了 using 语句来自动释放资源,并且在执行查询之前先打开了数据库连接。你需要将 connectionString 替换为你实际的连接字符串。
阅读全文