The fastest way to upload data to the Sql CE database on Windows Mobile 5

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)
                    {
                        //Do an Update
                        sqlCeResultSet.Read();

                        PopulateForUpdate(sqlCeResultSet, entityToSave);

                        sqlCeResultSet.Update();
                    }
                    else
                    {
                        //Do an Insert
                        SqlCeUpdatableRecord insertRecord = sqlCeResultSet.CreateRecord();

                        PopulateForInsert(insertRecord, entityToSave);

                        sqlCeResultSet.Insert(insertRecord);
                    }
                }
            }
        }
    }
+3
source share
1 answer

TableDirect , , 10- , . , , , . SqlCeResultSet .

+1

Source: https://habr.com/ru/post/1794948/


All Articles