在我的 Windows Strore 应用程序中,我在隔离存储中有 SQLite 数据库。
我使用sqlite-net库。
为了将数据保存到数据库,我使用以下方法:
public static async Task Save<T>(List<T> items)
{
var serverFolder = await ApplicationData.Current.LocalFolder.GetFolderAsync("FolgerName");
var storageFile = await serverFolder.GetFileAsync("DataBaseName.sqlite");
using (var db = new SQLiteConnection(storageFile.Path))
{
Debug.WriteLine("Save to DataBase " + (typeof(T)) + " started " + DateTime.Now.Minute + ":" + DateTime.Now.Second + ":" + DateTime.Now.Millisecond);
try
{
foreach (var item in items)
{
db.InsertOrReplace(item);
}
Debug.WriteLine("Save to DataBase " + (typeof(T)) + " success " + DateTime.Now.Minute + ":" + DateTime.Now.Second + ":" + DateTime.Now.Millisecond);
}
catch
(SQLiteException ex)
{
Debug.WriteLine("Save to DataBase " + (typeof(T)) + " error. Result: " + ex.Result + " Message: " + ex.Message);
}
}
}
我对性能有疑问:
1226 个对象,每个对象有 60 个字段,在 4 分半钟内保存在数据库中!
这是正常的?
我怎样才能让它更快?
更新
当我使用:
public static async Task Save<T>(List<T> items, Server server) where T : IHasId
{
var serverFolder = await ApplicationData.Current.LocalFolder.GetFolderAsync(server.FolgerName);
var storageFile = await serverFolder.GetFileAsync("kaliti.sqlite");
using (var db = new SQLiteConnection(storageFile.Path))
{
Debug.WriteLine("Save to DataBase " + (typeof(T)) + " started" + String.Format("{0:d/M/yyyy HH:mm:ss}", DateTime.Now));
try
{
var table = db.Table<T>();
var idList = new List<int>();
foreach (var i in table)
{
idList.Add(i.Id);
}
foreach (var item in items)
{
if (idList.Contains(item.Id))
{
db.Delete<T>(item.Id);
}
db.Insert(item);
}
Debug.WriteLine("Save to DataBase " + (typeof(T)) + " success" + String.Format("{0:d/M/yyyy HH:mm:ss}", DateTime.Now));
}
catch
(SQLiteException ex)
{
Debug.WriteLine("Save to DataBase " + (typeof(T)) + " error. Result: " + ex.Result + " Message: " + ex.Message);
}
}
}
和数据库是空的我有同样的东西。