C# C列表重新分配时速度较慢
我正在使用VS2008、C、Sqlite和ObjectListView。我有一个使用datareader查询数据库的方法,然后返回列表并分配给其他列表。为什么当我调用该方法并重新分配给List时,即使清除或将其设置为null,也需要越来越多的时间来重新分配。我用秒表测试它。第一次分配大约需要300毫秒,第二次分配大约800毫秒,第三次分配超过1000毫秒,然后继续上升。顺便说一句,对不起我的英语C# C列表重新分配时速度较慢,c#,list,sqlite,visual-studio-2008,C#,List,Sqlite,Visual Studio 2008,我正在使用VS2008、C、Sqlite和ObjectListView。我有一个使用datareader查询数据库的方法,然后返回列表并分配给其他列表。为什么当我调用该方法并重新分配给List时,即使清除或将其设置为null,也需要越来越多的时间来重新分配。我用秒表测试它。第一次分配大约需要300毫秒,第二次分配大约800毫秒,第三次分配超过1000毫秒,然后继续上升。顺便说一句,对不起我的英语 public static List<Document> GetCompanyDocum
public static List<Document> GetCompanyDocument(int companyId)
{
List<Document> docs = new List<Document>();
using (IDSDataAccess.IDSDataSqlite db = new IDSDataAccess.IDSDataSqlite())
{
StringBuilder sb = new StringBuilder();
sb.Append("SELECT document.*, prep.userInitial AS userPrep, Rev.userInitial AS userInitial ")
.Append("FROM document ")
// more sql command
.Append("WHERE document.compId = @compId ")
.Append("ORDER BY parent ASC, name ASC;");
db.CommandText = sb.ToString();
db.AddParameter("@compId", DbType.Int32, companyId);
db.CommandType = CommandType.Text;
db.Open();
db.ExecuteReader();
using (System.Data.SQLite.SQLiteDataReader reader = db.DbDataReader as SQLiteDataReader)
{
if (reader.HasRows)
{
while (reader.Read())
{
Document doc = new Document();
// do more action
docs.Add(doc);
}
}
if (reader != null)
{
if (!reader.IsClosed)
{
reader.Close();
reader.Dispose();
}
}
}
sb.Remove(0, sb.Length);
sb = null;
db.Close();
}
return docs;
}
// Other method
List<Document> docs = GetCompanyDocument(1);
是分配需要更长的时间还是检索查询结果?我猜由于某种原因,实际的查询需要更长的时间。您使用什么代码来测试它需要多长时间?测试是否包括从数据库检索查询响应所需的时间?sb.Remove…,sb=null;和db.Close部分都可以被删除——以及读卡器显式关闭。信任垃圾收集器并使用语句。我使用System.Diagnostics.Stopwatch并使用ElapsedMilliseconds。我尝试测试查询时间,对于一些测试,查询需要大约300毫秒。我认为查询是稳定的。你的if阅读器不应该吗!=null是否在if reader.HasRows之外?