Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#SQLite读取器在for循环中运行缓慢_C#_Sql_Database_Sqlite - Fatal编程技术网

C#SQLite读取器在for循环中运行缓慢

C#SQLite读取器在for循环中运行缓慢,c#,sql,database,sqlite,C#,Sql,Database,Sqlite,我正在使用数据读取器从sqlite数据库检索数据。 这是在双for循环中执行的。但是它非常慢,因为现在它要多次执行sql查询 有没有更快捷的方法? 以下是我现在所做工作的简短版本: for(int i=0;i < horizontal; i++) { for(int j=0;j < vertical; j++) { SQLiteConnection con = new SQLiteConnection(dbConnection); con.Open();

我正在使用数据读取器从sqlite数据库检索数据。 这是在双for循环中执行的。但是它非常慢,因为现在它要多次执行sql查询

有没有更快捷的方法? 以下是我现在所做工作的简短版本:

for(int i=0;i < horizontal; i++)
{
  for(int j=0;j < vertical; j++)
  {
    SQLiteConnection con = new SQLiteConnection(dbConnection);
    con.Open();
    SQLiteCommand cmd = new SQLiteCommand(con);
    cmd.CommandText = query;        
    SQLiteDataReader reader = cmd.ExecuteReader();
    dt.Load(reader);
    con.Close();

    // Perform some actions onto DataTable data
  }
}
for(int i=0;i
编辑: 在循环之前打开和关闭没有多大帮助。
我试图查询表中位置列位于4个点之间的所有行,对其执行一些计算并移动到下一个单元格(考虑网格)。

如果使用相同的连接,则可以创建SQLiteConnection对象并打开上面的连接,您还应该关闭迭代下的连接。它应该更快。

SQLiteConnection con=newsqliteconnection(dbConnection);
SQLiteConnection con = new SQLiteConnection(dbConnection);
con.Open();
SQLiteCommand cmd = new SQLiteCommand(con);
for(int i=0;i < horizontal; i++)
{
  for(int j=0;j < vertical; j++)
  {
    cmd.CommandText = query;    
    SQLiteDataReader reader = cmd.ExecuteReader();
    dt.Load(reader);
    // Perform some actions onto DataTable data
  }
}
con.Close();
con.Open(); SQLiteCommand cmd=新的SQLiteCommand(con); 对于(int i=0;i

只更改命令文本,无需每次都引用一个新文本。另外,只有在完成查询后才能关闭它。

是否使用Finisar.SQLite执行

如果是,请尝试使用System.Data.SQLite更改为

这对我很有帮助,表单6s减少到150毫秒。

无需在循环中频繁创建SQLiteConnection对象。在循环之前创建它,并在循环之后关闭con。尝试过但不是真正的解决方案。问题在于执行水平*垂直时间的查询。但是我不知道如何才能减少这种情况。您能展示SQL查询并解释嵌套循环的用途以及它如何影响查询吗?嵌套循环影响从数据库中选择的数据(其中x在xmin和xmax之间,y在ymin和ymax之间)。