我有一個在C#.NET和SQLite數據庫中實現的應用程序。我需要計算數據庫表(水果)的列(狀態)的一些值。表格如下。SQLite數據庫在C#.NET中查詢結果不正確
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'的結果用於診斷目的。 –