C# 使用station后,Sqlite数据库保持打开状态并锁定文件
我对一些sqlite代码有一个问题,下面的函数中似乎有什么东西导致数据库在应该被using语句关闭后保持打开状态,我可以通过调用函数中的GC.collect()来解决这个问题,但我想尝试找出问题的原因。我已经尝试在using station之后添加对GC.Collect()的调用,但这似乎不够,假设有什么东西阻止GC清除C# 使用station后,Sqlite数据库保持打开状态并锁定文件,c#,sqlite,garbage-collection,C#,Sqlite,Garbage Collection,我对一些sqlite代码有一个问题,下面的函数中似乎有什么东西导致数据库在应该被using语句关闭后保持打开状态,我可以通过调用函数中的GC.collect()来解决这个问题,但我想尝试找出问题的原因。我已经尝试在using station之后添加对GC.Collect()的调用,但这似乎不够,假设有什么东西阻止GC清除 /// <summary> /// Gets the Points of Interest for a given Category ///
/// <summary>
/// Gets the Points of Interest for a given Category
/// </summary>
/// <param name="rootPath">Target Path</param>
/// <param name="category">Category to search</param>
/// <returns>A collection of Points of interest</returns>
public static Collection<PointOfInterest> GetPointsOfInterest(string rootPath, PointOfInterestCategory category)
{
if (category == null)
{
throw new ArgumentNullException("category");
}
Collection<PointOfInterest> pointsOfInterest = new Collection<PointOfInterest>();
string databaseLocation = string.Format(Resources.DataFilePath, rootPath, "poidata.db");
using (SQLiteConnection connection = new SQLiteConnection(string.Format("Data Source={0};Version=3;", databaseLocation)))
{
connection.Open();
using (SQLiteCommand command = new SQLiteCommand(
"SELECT latmin + (latmax - latmin / 2) as lat, lonmin + (lonmax - lonmin / 2) as lon, city, street, housenr, name FROM poicoord JOIN poidata ON poicoord.poiid = poidata.poiid JOIN poiname on poiname.docid = poicoord.poiid WHERE type = @category",
connection))
{
command.Parameters.Add(new SQLiteParameter("category", DbType.Int32) { Value = category.Id });
SQLiteDataReader reader = command.ExecuteReader();
int latOrdinal = reader.GetOrdinal("lat");
int lonOrdinal = reader.GetOrdinal("lon");
int nameOrdinal = reader.GetOrdinal("name");
int houseNrOrdinal = reader.GetOrdinal("housenr");
int streetOrdinal = reader.GetOrdinal("street");
int cityOrdinal = reader.GetOrdinal("city");
while (reader.Read())
{
pointsOfInterest.Add(new PointOfInterest()
{
Latitude = reader.GetDouble(latOrdinal),
Longitude = reader.GetDouble(lonOrdinal),
Name = reader.GetString(nameOrdinal),
HouseNumber = reader.IsDBNull(houseNrOrdinal) ? string.Empty : reader.GetString(houseNrOrdinal),
Street = reader.IsDBNull(streetOrdinal) ? string.Empty : reader.GetString(streetOrdinal),
City = reader.IsDBNull(cityOrdinal) ? string.Empty : reader.GetString(cityOrdinal),
});
}
}
}
return pointsOfInterest;
}
//
///获取给定类别的兴趣点
///
///目标路径
///要搜索的类别
///兴趣点的集合
公共静态集合GetPointsOfInterest(字符串根路径、PointOfInterestCategory类别)
{
如果(类别==null)
{
抛出新的异常(“类别”);
}
集合点兴趣=新集合();
stringdatabaselocation=string.Format(Resources.DataFilePath,rootPath,“poidata.db”);
使用(SQLiteConnection=newsqliteconnection(string.Format(“数据源={0};版本=3;”,databaseLocation)))
{
connection.Open();
使用(SQLiteCommand=newsqlitecommand)(
“选择latmin+(latmax-latmin/2)作为lat,选择lonmin+(lonmax-lonmin/2)作为lon、城市、街道、房屋编号、poicoord上的poidata中的poidata中的名称。POIDID=poidata.POIDJOIN poiname在poiname.docid=poicoord.POIDwhere type=@category”,
连接)
{
Add(新的SQLiteParameter(“category”,DbType.Int32){Value=category.Id});
SQLiteDataReader=command.ExecuteReader();
int-latOrdinal=reader.GetOrdinal(“lat”);
int lonOrdinal=reader.GetOrdinal(“lon”);
int nameOrdinal=reader.GetOrdinal(“名称”);
int housenrodinal=reader.GetOrdinal(“housenr”);
int streetOrdinal=reader.GetOrdinal(“street”);
int cityOrdinal=reader.GetOrdinal(“城市”);
while(reader.Read())
{
添加(新的兴趣点()
{
纬度=读卡器.GetDouble(latOrdinal),
经度=reader.GetDouble(lonOrdinal),
Name=reader.GetString(nameOrdinal),
HouseNumber=reader.IsDBNull(housenrodinal)?string.Empty:reader.GetString(housenrodinal),
Street=reader.IsDBNull(streetOrdinal)?string.Empty:reader.GetString(streetOrdinal),
City=reader.IsDBNull(cityrdinal)?string.Empty:reader.GetString(cityrdinal),
});
}
}
}
返回兴趣点;
}
尝试使用语句实现,而不仅仅是将读取器分配给变量:
using (SQLiteDataReader reader=command.ExecuteReader()) {
...
}
或手动关闭读卡器:
reader.Close();
`SQLiteDataReader reader`也可以在using周围包装它,或者在离开内部using
之前执行类似于((IDisposable)reader.Dispose()的操作代码>你只需要关闭你的阅读器