好的,我的计数确实有效,这意味着在添加和删除数据时它会显示正确的数字。
问题是,如果我只添加一个条目,我会显示我有1个条目,但如果我删除了唯一的条目,我的计数器仍然显示1.为什么不是0?
我就是这样做的(我更新我的面板标题的字段在底部):
public void DisplayFileContent(string filePath)
{
lvPC.Items.Clear();
lvWeb.Items.Clear();
lvSerialCode.Items.Clear();
string hashShortPass = hash.ShortHash(storedAuth.Password);
// Counting all entries.
int countEntries = 0;
int countPC = 0;
int countWeb = 0;
int countSerial = 0;
string connectionString =
@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source={0};
Persist Security Info=False; Jet OLEDB:Database Password={1};";
// Encrypting/Decrypting data.
Security security = new Security();
using (OleDbConnection connection = new OleDbConnection())
{
connection.ConnectionString = string.Format(connectionString, filePath, hashShortPass);
using (OleDbCommand command = new OleDbCommand
("Select * FROM PersonalData", connection))
{
try
{
// Open database connection.
connection.Open();
using (OleDbDataReader read = command.ExecuteReader())
{
// Checking if there is any data in the file.
if (read.HasRows)
{
// Reading information from the file.
while (read.Read())
{
// Count all entries read from the reader.
countEntries++;
if (security.DecryptAES(read.GetString(1),
storedAuth.Password, storedAuth.UserName)
== "PC Password")
{
// Count PC passwords.
countPC++;
PC.Tag = read.GetInt32(0);
lvPC.Items.Add(PC);
}
else if (security.DecryptAES(read.GetString(1),
storedAuth.Password, storedAuth.UserName)
== "Web Site Password")
{
countWeb++;
Web.Tag = read.GetInt32(0);
lvWeb.Items.Add(Web);
}
else if (security.DecryptAES(read.GetString(1),
storedAuth.Password, storedAuth.UserName)
== "Serial Code")
{
countSerial++;
Serial.Tag = read.GetInt32(0);
}
tabPage1.Text = "PC Passwords ( " + countPC + " )";
tabPage2.Text = "Web Site Passwords ( " + countWeb + " )";
tabPage3.Text = "Serial Codes ( " + countSerial + " )";
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
}
}
答案 0 :(得分:2)
问题是如果删除条目,则不会执行更新标签的代码。
您需要移动以下块:
tabPage1.Text = "PC Passwords ( " + countPC + " )";
tabPage2.Text = "Web Site Passwords ( " + countWeb + " )";
tabPage3.Text = "Serial Codes ( " + countSerial + " )";
在与此声明关联的}
之后:
if (read.HasRows)
此外,由于不需要HasRows,您可以将其重写为:
using (OleDbDataReader read = command.ExecuteReader())
{
// Reading information from the file.
while (read.Read())
{
// Count all entries read from the reader.
countEntries++;
if (security.DecryptAES(read.GetString(1),
storedAuth.Password, storedAuth.UserName)
== "PC Password")
{
// Count PC passwords.
countPC++;
PC.Tag = read.GetInt32(0);
lvPC.Items.Add(PC);
}
else if (security.DecryptAES(read.GetString(1),
storedAuth.Password, storedAuth.UserName)
== "Web Site Password")
{
countWeb++;
Web.Tag = read.GetInt32(0);
lvWeb.Items.Add(Web);
}
else if (security.DecryptAES(read.GetString(1),
storedAuth.Password, storedAuth.UserName)
== "Serial Code")
{
countSerial++;
Serial.Tag = read.GetInt32(0);
}
}
}
tabPage1.Text = "PC Passwords ( " + countPC + " )";
tabPage2.Text = "Web Site Passwords ( " + countWeb + " )";
tabPage3.Text = "Serial Codes ( " + countSerial + " )";