我在Windows Mobile v6上使用sqlite作为我的.NET Compact Framework 3.5应用程序。如果我插入大数据,我将面临缓慢问题。
考虑这种情况:我的ArrayList
中有大约150个项目(比如List1
)。我有一个包含两列的数据库表(例如itemid
和isFound
)。
我必须迭代List1
中的每个元素,并在我的数据库中检查itemid是否可用
为此,最快的方法是花费大约30秒?
这是我目前的代码:
public ArrayList InsertNewlyScannedItems(ref ArrayList newlyScannedItemList)
{
ArrayList newInsertedItemList = new ArrayList();
SQLiteConnection conn = new SQLiteConnection("Data Source=" + db + ";Version=3;");
SQLiteDataReader rs = null;
try
{
conn.Open();
SQLiteCommand availableTable = conn.CreateCommand();
IEnumerator en = newlyScannedItemList.GetEnumerator();
while (en.MoveNext())
{
ItemInfo itmInfo = (ItemInfo)en.Current;
string cmdText = "";
cmdText = "Select id, isFound item where id = '" + itmInfo.id + "'";
availableTable.CommandText = cmdText;
rs = availableTable.ExecuteReader();
if (rs.Read())
{
if (!itmInfo.id.Equals(""))
{
availableTable.Dispose();
availableTable.CommandText = "UPDATE item SET isFound = @isFound Where id = @id";
availableTable.Parameters.AddWithValue("@isFound", itmInfo.isFound);
availableTable.Parameters.AddWithValue("@id", itmInfo.id);
availableTable.ExecuteNonQuery();
}
}
else
{
availableTable.Dispose();
cmdText = "INSERT INTO item(id, isFound)";
cmdText += "VALUES ( '" + itmInfo.id + "','" + itmInfo.isFound + ")";
availableTable.CommandText = cmdText;
availableTable.ExecuteNonQuery();
}
newInsertedItemList.Add(itmInfo);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (rs != null)
{
//rs.Close();
//rs.Dispose();
}
if (conn != null && conn.State == ConnectionState.Open)
{
conn.Close();
conn = null;
}
}
return newInsertedItemList;
}