C# 插入到隔离存储性能中的Sqlite数据库
在我的Windows Strore应用程序中,我的SQLite数据库位于独立存储中。C# 插入到隔离存储性能中的Sqlite数据库,c#,sqlite,windows-store-apps,C#,Sqlite,Windows Store Apps,在我的Windows Strore应用程序中,我的SQLite数据库位于独立存储中。 我使用图书馆。 对于将数据保存到数据库,我使用以下命令: public static async Task Save<T>(List<T> items) { var serverFolder = await ApplicationData.Current.LocalFolder.GetFolderAsync("FolgerName"); var
我使用图书馆。
对于将数据保存到数据库,我使用以下命令:
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);
}
}
}
公共静态异步任务保存(列表项)
{
var serverFolder=wait ApplicationData.Current.LocalFolder.GetFolderAsync(“FolgerName”);
var storageFile=await serverFolder.GetFileAsync(“DataBaseName.sqlite”);
使用(var db=new-SQLiteConnection(storageFile.Path))
{
Debug.WriteLine(“保存到数据库”+(typeof(T))+“开始”+DateTime.Now.Minute+”:“+DateTime.Now.Second+”:“+DateTime.Now.毫秒);
尝试
{
foreach(项目中的var项目)
{
db.插入或替换(项目);
}
Debug.WriteLine(“保存到数据库”+(typeof(T))+“成功”+DateTime.Now.Minute+”:“+DateTime.Now.Second+”:“+DateTime.Now.毫秒);
}
抓住
(SQLiteException-ex)
{
Debug.WriteLine(“保存到数据库”+(typeof(T))+”错误。结果:“+ex.Result+”消息:“+ex.Message”);
}
}
}
我对性能有疑问:1226个对象,每个对象中有60个字段,在4.5分钟内保存到数据库中
正常吗?
我怎样才能使它更快 更新 当我使用:
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);
}
}
}
公共静态异步任务保存(列表项,服务器),其中T:IHasId
{
var serverFolder=wait ApplicationData.Current.LocalFolder.GetFolderAsync(server.FolgerName);
var storageFile=await serverFolder.GetFileAsync(“kaliti.sqlite”);
使用(var db=new-SQLiteConnection(storageFile.Path))
{
WriteLine(“保存到数据库”+(typeof(T))+“开始”+String.Format(“{0:d/M/yyyy HH:mm:ss}”,DateTime.Now));
尝试
{
var table=db.table();
var idList=新列表();
foreach(表中的var i)
{
idList.Add(i.Id);
}
foreach(项目中的var项目)
{
if(idList.Contains(item.Id))
{
db.删除(项目Id);
}
db.插入(项目);
}
WriteLine(“保存到数据库”+(typeof(T))+“成功”+String.Format(“{0:d/M/yyyy HH:mm:ss}”,DateTime.Now));
}
抓住
(SQLiteException-ex)
{
Debug.WriteLine(“保存到数据库”+(typeof(T))+”错误。结果:“+ex.Result+”消息:“+ex.Message”);
}
}
}
数据库是空的,我也有同样的东西。您需要在事务内部运行尽可能多的数据库调用。这样做可以大大加快速度。下面是一个示例事务,使用第一个示例中的代码:
...
try
{
db.RunInTransaction(() =>
{
foreach (var item in items)
{
db.InsertOrReplace(item);
}
});
...
您需要在事务内部运行尽可能多的数据库调用。这样做可以大大加快速度。下面是一个示例事务,使用第一个示例中的代码:
...
try
{
db.RunInTransaction(() =>
{
foreach (var item in items)
{
db.InsertOrReplace(item);
}
});
...
InsertOrReplace的代码在这里很关键,但当数据库为空时,我只使用Insert-我有相同的东西!((InsertOrReplace的代码在这里很关键,好吧,但是当数据库为空时,我只使用Insert-我有相同的东西!((优雅的方式。谢谢。我没有4分半钟,现在有几秒钟。优雅的方式。谢谢。我没有4分半钟,现在有几秒钟。)。