【发布时间】:2015-01-06 15:57:23
【问题描述】:
我有一个用 C#.NET 和 SQLite 数据库实现的应用程序。我需要计算数据库表(水果)的列(状态)的一些值。表格如下。
Id B M status
1 10 101 2
2 11 102 2
3 11 103 0
4 12 104 2
5 13 105 2
6 13 105 2
7 14 106 2
8 14 106 2
9 14 106 2
10 14 106 2
我使用以下方法从表中提取值并显示输出。
public void CountingValues()
{
string strA, strB, strC;
double Total;
strA = countA();
strB = countB();
strC = countC();
MessageBox.Show(strA.ToString());
MessageBox.Show(strB.ToString());
MessageBox.Show(strC.ToString());
}
public string countA()
{
return GetData("SELECT COUNT(status) FROM fruit WHERE status = 2;");
}
public string countB()
{
return GetData("SELECT COUNT(status) FROM fruit WHERE status = 1;");
}
public string countC()
{
return GetData("SELECT COUNT(status) FROM fruit WHERE status = 0;");
}
private String GetData(String cmd)
{
String result = "";
SQLiteCommand command = new SQLiteCommand(connection);
command.CommandText = cmd;
SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
{
result = reader[0].ToString();
}
reader.Dispose();
command.Dispose();
return result;
}
strA、strB 和 strC 的结果应该分别为 9、0 和 1。但是,它显示的是 4、0 和 6。 谁能告诉我这里出了什么问题?
【问题讨论】:
-
我建议您使用替代查询来显示 .NET 读取的数据库内容。我的猜测是您不小心附加到了错误的数据库。
-
我的数据库是在应用程序运行时上传的。因此,没有错误的数据库连接的可能性。并且,您在谈论哪个替代查询?你能解释一下吗?
-
我建议转储
SELECT * FROM fruit的结果以用于诊断目的。 -
我使用 SELECT * FROM fruit where status = 2. 但它显示的结果相同。
-
那么您需要更改其余代码,以读取所有列和所有行...