gpt4 book ai didi

c# - C# 应用程序的 SQlite 部分耗时太长

转载 作者:太空宇宙 更新时间:2023-11-03 21:39:27 26 4
gpt4 key购买 nike

<分区>

我正在使用 c# winforms 和 sqlite 作为我的数据库制作一个应用程序。

它只是读取文本文件,将它们传输到数据库,解析它们,然后显示用户输出。

问题:SQlite 部分非常慢。运行与填充数据库相关的代码大约需要 3 分钟!

我是编程新手,所以我真的不知道关键问题是什么。除了尝试优化我的代码以仅包含所需内容之外,是否有任何工具可以帮助提高应用程序速度、清理后台垃圾等?

这是应用程序的缓慢部分:

 private void button4_Click(object sender, EventArgs e)
{

SQLiteConnection sqlite_conn;
SQLiteCommand sqlite_cmd;
SQLiteDataReader sqlite_datareader;


sqlite_conn = new SQLiteConnection(@"Data Source=database.db;Version=3;");


sqlite_conn.Open();

// create a new SQL command:
sqlite_cmd = sqlite_conn.CreateCommand();


sqlite_cmd.CommandText = "CREATE TABLE IF NOT EXISTS 'abc' (Seq text, Field text, Desc text, Len text, Dec text, Typ text, Percnt text, Pop text, Alzero text, MaxLen text );";


sqlite_cmd.ExecuteNonQuery();

// **** SQLITE TRANSFER SECTION 1 - transfer values from list1 to table1 *****

sqlite_cmd.CommandText = " DELETE FROM abc";
sqlite_cmd.ExecuteNonQuery();

sqlite_cmd.CommandText = "INSERT INTO abc (Seq, Field, Desc, Len, Dec, Typ, Percnt, Pop, Alzero, MaxLen) VALUES (@p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10)";
sqlite_cmd.Parameters.AddWithValue("@p1", 6); // dummy initial values
sqlite_cmd.Parameters.AddWithValue("@p2", 878);
sqlite_cmd.Parameters.AddWithValue("@p3", 56);
sqlite_cmd.Parameters.AddWithValue("@p4", 6);
sqlite_cmd.Parameters.AddWithValue("@p5", 546);
sqlite_cmd.Parameters.AddWithValue("@p6", 565);
sqlite_cmd.Parameters.AddWithValue("@p7", 568);
sqlite_cmd.Parameters.AddWithValue("@p8", 526);
sqlite_cmd.Parameters.AddWithValue("@p9", 586);
sqlite_cmd.Parameters.AddWithValue("@p10", 526);


for (int i = 0; i < NumListValues; i += 10) // Filling SQlite table rows and columns with values from our list
{
sqlite_cmd.Parameters.AddWithValue("@p1", list[i]);
sqlite_cmd.Parameters.AddWithValue("@p2", list[i+1]);
sqlite_cmd.Parameters.AddWithValue("@p3", list[i+2]);
sqlite_cmd.Parameters.AddWithValue("@p4", list[i+3]);
sqlite_cmd.Parameters.AddWithValue("@p5", list[i+4]);
if (i > 490)
break;
sqlite_cmd.Parameters.AddWithValue("@p6", list[i+5]);
sqlite_cmd.Parameters.AddWithValue("@p7", list[i+6]);
sqlite_cmd.Parameters.AddWithValue("@p8", list[i+7]);
sqlite_cmd.Parameters.AddWithValue("@p9", list[i+8]);
sqlite_cmd.Parameters.AddWithValue("@p10", list[i+9]);
sqlite_cmd.ExecuteNonQuery();

}

// **** SQLITE TRANSFER SECTION 2 - transfer values from list2 to 2nd table *****

sqlite_cmd.CommandText = "CREATE TABLE IF NOT EXISTS 't2' (YYMM text, MinDate text, MaxDate text, TotalTrans text, DebitTrans text, AMOUNTINDOCUMENTCURREN text );";

sqlite_cmd.ExecuteNonQuery();

sqlite_cmd.CommandText = " DELETE FROM t2";
sqlite_cmd.ExecuteNonQuery();

sqlite_cmd.CommandText = "INSERT INTO t2 (YYMM, MinDate, MaxDate, TotalTrans, DebitTrans, AMOUNTINDOCUMENTCURREN ) VALUES (@b1, @b2, @b3, @b4, @b5, @b6)";
sqlite_cmd.Parameters.AddWithValue("@b1", 6); // dummy initial values
sqlite_cmd.Parameters.AddWithValue("@b2", 878);
sqlite_cmd.Parameters.AddWithValue("@b3", 56);
sqlite_cmd.Parameters.AddWithValue("@b4", 6);
sqlite_cmd.Parameters.AddWithValue("@b5", 546);
sqlite_cmd.Parameters.AddWithValue("@b6", 565);

for (int i = 0; i < NumList2Values; i+= 6) // Filling SQlite table rows and columns with values from list2
{
sqlite_cmd.Parameters.AddWithValue("@b1", list2[i]);
sqlite_cmd.Parameters.AddWithValue("@b2", list2[i+1]);
sqlite_cmd.Parameters.AddWithValue("@b3", list2[i+2]);
sqlite_cmd.Parameters.AddWithValue("@b4", list2[i+3]);
sqlite_cmd.Parameters.AddWithValue("@b5", list2[i+4]);
sqlite_cmd.Parameters.AddWithValue("@b6", list2[i+5]);
sqlite_cmd.ExecuteNonQuery();
}



// Create table to transfer values from list 3

sqlite_cmd.CommandText = "CREATE TABLE IF NOT EXISTS 't3' (YYWW text, MinDate text, MaxDate text, TotalTrans text, DebitTrans text, AMOUNTINDOCUMENTCURREN text );";

sqlite_cmd.ExecuteNonQuery();

sqlite_cmd.CommandText = " DELETE FROM t3";
sqlite_cmd.ExecuteNonQuery();

sqlite_cmd.CommandText = "INSERT INTO t3 (YYWW, MinDate, MaxDate, TotalTrans, DebitTrans, AMOUNTINDOCUMENTCURREN ) VALUES (@c1, @c2, @c3, @c4, @c5, @c6)";
sqlite_cmd.Parameters.AddWithValue("@c1", 6); // dummy initial values
sqlite_cmd.Parameters.AddWithValue("@c2", 878);
sqlite_cmd.Parameters.AddWithValue("@c3", 56);
sqlite_cmd.Parameters.AddWithValue("@c4", 6);
sqlite_cmd.Parameters.AddWithValue("@c5", 546);
sqlite_cmd.Parameters.AddWithValue("@c6", 565);

for (int i = 0; i < NumList3Values ; i+= 6) // Filling SQlite table rows and columns with values from list2
{
sqlite_cmd.Parameters.AddWithValue("@c1", list3[i]);
sqlite_cmd.Parameters.AddWithValue("@c2", list3[i+1]);
sqlite_cmd.Parameters.AddWithValue("@c3", list3[i+2]);
sqlite_cmd.Parameters.AddWithValue("@c4", list3[i+3]);
sqlite_cmd.Parameters.AddWithValue("@c5", list3[i+4]);
sqlite_cmd.Parameters.AddWithValue("@c6", list3[i+5]);
sqlite_cmd.ExecuteNonQuery();

}
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com