有没有一种方法可以加快SQLite C#上的数据导入

时间:2019-07-02 07:18:57

标签: c#

我正在开发气象应用程序,但是数据导入非常缓慢。
我尝试使用“开始”和“结束”交易,但是过程仍然很慢。

        foreach (string file in files)
        {
            var formattedName = Path.GetFileName(file).Split('_').First();
            foreach (var item in result)
            {
                listBox2.Items.Add($"{item.date}".ToString());

                const string insertData = "INSERT OR IGNORE INTO meteodata (Station, Dat, Temp, Hum, Press, windSpeed, windDirection, Rain, rainIntensity, notDraw, notDraw1, sunRad) " +
                "values (@Station, @Dat, @Temp, @Hum, @Press, @windSpeed, @windDirection, @Rain, @rainIntensity, @notDraw, @notDraw1, @sunRad)";

                SQLiteCommand fillData = new SQLiteCommand(insertData, Connection);

                fillData.Parameters.AddWithValue("@Station", formattedName);
                fillData.Parameters.AddWithValue("@Dat", item.date);
                fillData.Parameters.AddWithValue("@Temp", item.temperature);
                fillData.Parameters.AddWithValue("@Hum", item.hum);
                fillData.Parameters.AddWithValue("@Press", item.presure);
                fillData.Parameters.AddWithValue("@windSpeed", item.windSpeed);
                fillData.Parameters.AddWithValue("@windDirection", item.windDirect);
                fillData.Parameters.AddWithValue("@Rain", item.rain);
                fillData.Parameters.AddWithValue("@rainIntensity", item.rainIntensity);
                fillData.Parameters.AddWithValue("@notDraw", item.notDraw);
                fillData.Parameters.AddWithValue("@notDraw1", item.notDraw1);
                fillData.Parameters.AddWithValue("@sunRad", item.sunRadiation);

                SQLiteTransaction trans;

                Connection.Open();

                trans = Connection.BeginTransaction();

                SQLiteCommand sqlComm;
                sqlComm = new SQLiteCommand("begin", Connection);
                sqlComm = new SQLiteCommand(insertData, Connection);


                sqlComm = new SQLiteCommand("end", Connection);
                sqlComm.ExecuteNonQuery();
                Connection.Close();
            }
        }
    }
}

private void button1_Click(object sender, EventArgs e)
{
    if (comboBox1.Text == temp)
    {
        try
        {
            SQLiteTransaction trans;
            Connection.Open();
            trans = Connection.BeginTransaction();

            string selectTemp = "SELECT Temp FROM meteodata WHERE Station= " + comboBox2.SelectedItem.ToString() + " AND Dat BETWEEN '" + comboBox3.SelectedItem.ToString().Replace("_", "-") + "' AND '" + comboBox4.SelectedItem.ToString().Replace("_", "-") + "' ORDER by Dat";
            SQLiteCommand sqlComm;
            sqlComm = new SQLiteCommand("begin", Connection);
            using (SQLiteCommand cmd = new SQLiteCommand(selectTemp, Connection))
            {
                using (SQLiteDataReader dr = cmd.ExecuteReader())
                {
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            bool iii = dr.Read();
                            {

                                MessageBox.Show(dr["Temp"].ToString());

                            }
                        }
                        dr.Close();
                    }
                    else
                    {

                        dr.Close();
                    }
                }
            }
            sqlComm = new SQLiteCommand("end", Connection);
            Connection.Close();
        }
        catch (SQLiteException err)
        {
            MessageBox.Show("Caught exception: " + err.Message);
        }
        Connection.Close();
    }

我希望数据能够快速导入,以便用户可以选择查询。 顺便说一句,我想在while循环中在消息框上显示带有值的数据,但是它不起作用。我不知道为什么。

我决定使用SQLite,因为我读到不需要服务器。

0 个答案:

没有答案