I am developing an application on Windows Mobile 5, in which part of the application loads a data download (2000+) from IEnumerable.
I am currently doing a check to determine if an identifier exists in the database, and depending on the result, I execute the Insert or Update statement (using the ExecuteNonQuery method for SqlCeCommand).
This works fine so far, but I am wondering if there is a faster / better way to do this, which can improve performance.
I looked at some Bulk Insert, but from what I understand, this will only work for inserts. There is a good chance that some elements in my IEnumerable <> will already exist in the database, and if necessary, an update is required. So what Bulk Copy / Insert does not - is that right?
Any answers appreciated.
Many thanks,
ETFairfax.
EDIT: This is the code I ended up in. The performance improvement is amazing. I left him, taking 30 seconds to save 1000 records, and this is an instant. Feel free to suggest further improvements ....
public void MergeData(IEnumerable<T> entities)
{
using (var cmd = GlobalConnection.Instance.Connection.CreateCommand())
{
cmd.CommandType = CommandType.TableDirect;
cmd.IndexName = "PK_" + this.TableName;
cmd.CommandText = this.TableName;
using (SqlCeResultSet sqlCeResultSet = cmd.ExecuteResultSet(ResultSetOptions.Updatable))
{
foreach (var entityToSave in entities)
{
bool recordAlreadyExists = sqlCeResultSet.Seek(DbSeekOptions.FirstEqual, entityToSave.Id);
if (recordAlreadyExists)
{
sqlCeResultSet.Read();
PopulateForUpdate(sqlCeResultSet, entityToSave);
sqlCeResultSet.Update();
}
else
{
SqlCeUpdatableRecord insertRecord = sqlCeResultSet.CreateRecord();
PopulateForInsert(insertRecord, entityToSave);
sqlCeResultSet.Insert(insertRecord);
}
}
}
}
}
source
share