我有一个用C#编写的WPF项目,该项目使用sqlite数据库。
我尝试从表中删除我的一行,然后返回调用该删除的方法后,然后尝试获取该表中的所有行,并返回已删除的行。
奇怪的是,如果我尝试在执行删除操作后立即返回所有行,但不返回,那么它将返回所有行减去我删除的行, 但是如果我返回调用方法,然后尝试返回所有行,则会显示已删除的行。
private void RemoveTagScannersButtonClicked(object sender, RoutedEventArgs e)
{
try
{
TagScanner tagScanner = (TagScanner)dgTagScanners.SelectedItem;
ScannerDAO scannerDAO = new ScannerDAO();
scannerDAO.DeleteScanner(tagScanner.Id);
//here is where I load the scanners after deleting the scanner row in the database
//and the deleted scanner row is returned dispite the fact that I just deleted it.
LoadScannerInformation();
}
catch(Exception exc)
{
Console.WriteLine("test");
}
}
internal bool DeleteScanner(int id)
{
int result = -1;
string connectionString = "Data Source=" + LocationManager.DatabaseLocation + "\\LapCounter.sqlite";
lock (ConnectionManager.instanceLock)
{
using (SQLiteConnection conn = ConnectionManager.CreateConnection(connectionString))
{
using (SQLiteCommand cmd = new SQLiteCommand(conn))
{
cmd.CommandText = "DELETE FROM tagscanners WHERE id=@id";
cmd.Prepare();
cmd.Parameters.AddWithValue("@id", id);
try
{
result = cmd.ExecuteNonQuery();
if (result == 0)
{
return false;
}
}
catch (SQLiteException e)
{
return false;
}
}
conn.Close();
}
}
/*I added next line because when I return to the calling method and call
GetTagScannersForRace(1) the returned rows include the deleted row, but if I call it
right below before I return to the calling method the returned rows does not contain
the deleted row
*/
//List<TagScanner> scanners = GetTagScannersForRace(1);
return true;
}
private void LoadScannerInformation()
{
List<TagScanner> scanners = getTagScanners();
dgTagScanners.ItemsSource = scanners;
}
List<TagScanner> getTagScanners()
{
if(_tagScanners.Count == 0)
{
ScannerDAO scannerDAO = new ScannerDAO();
_tagScanners = scannerDAO.GetTagScannersForRace(_raceId);
}
return _tagScanners;
}
public List<TagScanner> GetTagScannersForRace(int raceId)
{
string connectionString = "Data Source=" + LocationManager.DatabaseLocation + "\\LapCounter.sqlite";
List<TagScanner> scanners = new List<TagScanner>();
try
{
lock (ConnectionManager.instanceLock)
{
using (SQLiteConnection conn = ConnectionManager.CreateConnection(connectionString))
{
using (SQLiteCommand cmd = new SQLiteCommand(conn))
{
cmd.CommandText = "SELECT * FROM tagscanners " +
"WHERE raceid=@raceid";
cmd.Prepare();
cmd.Parameters.AddWithValue("@raceid", raceId);
using (SQLiteDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
TagScanner currentScanner = new TagScanner();
currentScanner.Id = Int32.Parse(reader["id"].ToString());
currentScanner.TagScannerIp = reader["tagscannerip"].ToString();
scanners.Add(currentScanner);
}
}
}
conn.Close();
}
}
}
catch (SQLiteException e)
{
return scanners;
}
return scanners;
}
谁能解释为什么我删除该行后立即选择所有行返回所有行减去已删除的行, 但是如果我返回调用方法并选择所有行,它将返回所有行,包括已删除的行。
答案 0 :(得分:0)
Marc Gravell的评论向我指出了正确的方向。
我删除了数据库,删除后,我的代码为正在运行的应用程序创建了一个新代码。
现在,我的删除实际上确实显示在表中,因此我假设它是损坏的数据库。我从未在其他任何RDBS上发生过这种情况(这是我第一次使用sqlite),所以我并没有真正将其视为一个问题。
谢谢马克。