使用C#执行速度非常慢的SQLite数据库

使用C#执行速度非常慢的SQLite数据库,c#,.net,database,sqlite,C#,.net,Database,Sqlite,我的sqlite数据库存储在C:\program name\db\db.s3db中。 我有一个表members,其中大约有70条记录。因此,现在我必须执行一个程序,将成员表中的所有账号复制到另一个表act\u monthly\u listings 这表明它正在执行,但速度非常慢。我不知道是不是因为我的代码。以下是一个片段: 连接: private void SetConnection() { sql_con = new SQLiteConnection

我的
sqlite
数据库存储在
C:\program name\db\db.s3db
中。 我有一个表
members
,其中大约有
70条记录。因此,现在我必须执行一个程序,将
成员
表中的所有账号复制到另一个表
act\u monthly\u listings

这表明它正在执行,但速度非常慢。我不知道是不是因为我的代码。以下是一个片段:

连接:

private void SetConnection()
        {
            sql_con = new SQLiteConnection
                ("Data Source=C:\\credit-union\\db\\cu.s3db;Version=3;New=False;Compress=True;");
        }
在按钮上单击代码:

private void button1_Click(object sender, EventArgs e)
        {

        button1.Text = "Working Please Wait";
        string init_month = dtInit.Value.Month.ToString();
        string init_yr = dtInit.Value.Month.ToString();

        SetConnection();
        sql_con.Open();

        SQLiteCommand cmd = new SQLiteCommand();
        cmd.CommandText = "select ec_no from members";
        cmd.Connection = sql_con;

        DR = cmd.ExecuteReader();

        int count = 0;

        while (DR.Read())
        {
            DR.Read();
            this.Text = "Account : " + DR.GetInt32(0).ToString();
           string SQL = "INSERT INTO act_monthly_listings (year,month,act) VALUES ("+ init_yr + "," + init_month + "," + DR.GetInt32(0).ToString() + ")";
           //Clipboard.SetText(SQL)
           if (ExecuteQuery(SQL))
           {
               count++;
           }


        }

        if(count > 0){
            MessageBox.Show("Accounts Initialized!", "Success", MessageBoxButtons.OK,MessageBoxIcon.Information);
        }else{
            MessageBox.Show("Initialization Failed!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
        sql_con.Close(); 
    }

这是因为
在执行
insert
查询时等于行数,这使得程序运行非常慢,因此最好使用
插入-SELECT
类似:

     string init_month = dtInit.Value.Month.ToString();
     string init_yr = dtInit.Value.Month.ToString();
     SQLiteCommand cmd = new SQLiteCommand();
     cmd.CommandText = "INSERT INTO act_monthly_listings (year,month,act) select @y,@m,ec_no from members"; 
     cmd.Parameters.Add(new SQLiteParameter("@y", init_yr ));
     cmd.Parameters.Add(new SQLiteParameter("@m", init_month ));
     cmd.Connection = sql_con;
     cmd.ExecuteNonQuery();
使用
while
时插入的所有行,直到
reader.Read()
指针到达最后一行
DataReader
正在与DB(需要活动连接)连接,这可能会使连接池速度变慢,并且您正在按照
Read
执行查询,请避免这种情况,
假设您有一百万条记录,那么代码发生了什么?

这是因为在执行
时,您执行的
insert
查询等于行数,这使得程序运行非常慢,因此最好使用
插入-SELECT
类似:

     string init_month = dtInit.Value.Month.ToString();
     string init_yr = dtInit.Value.Month.ToString();
     SQLiteCommand cmd = new SQLiteCommand();
     cmd.CommandText = "INSERT INTO act_monthly_listings (year,month,act) select @y,@m,ec_no from members"; 
     cmd.Parameters.Add(new SQLiteParameter("@y", init_yr ));
     cmd.Parameters.Add(new SQLiteParameter("@m", init_month ));
     cmd.Connection = sql_con;
     cmd.ExecuteNonQuery();
使用
while
时插入的所有行,直到
reader.Read()
指针到达最后一行
DataReader
正在与DB(需要活动连接)连接,这可能会使连接池速度变慢,并且您正在按照
Read
执行查询,请避免这种情况,
假设你有一百万条记录,那么你的代码发生了什么?

这可能是因为
while
,为什么不使用
插入-选择
一次完成所有记录。如果成功了,你只会每隔一行得到一次,因为你调用了
读取()
两次。这可能是因为
while
,为什么不使用
INSERT-INTO-SELECT
在一次操作中完成所有操作。如果成功,则只能每隔一行执行一次,因为您调用了
Read()
两次。