Update/Insert to a table using SQLCeResultSet

前端 未结 1 1432
自闭症患者
自闭症患者 2021-02-09 01:58

I have a SQL Compact Edition Database that I update periodically (via web services).

The part where I write to the database is taking way too long. I am currently doing

1条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-09 02:56

    It's going to look something like this:

    (Edited for insert or update)

    void Foo(SqlCeConnection connection)
    {
        using (var cmd = new SqlCeCommand())
        {
            cmd.CommandType = CommandType.TableDirect;
            cmd.CommandText = "MyTableName";
            cmd.Connection = connection;
            cmd.IndexName = "PrimakryKeyIndexName";
    
            using (var result = cmd.ExecuteResultSet(
                                ResultSetOptions.Scrollable | ResultSetOptions.Updatable))
            {
                int pkValue = 100; // set this, obviously
    
                if (result.Seek(DbSeekOptions.FirstEqual, pkValue))
                {
                    // row exists, need to update
                    result.Read();
    
                    // set values
                    result.SetInt32(0, 1);
                    // etc. 
    
                    result.Update();
                }
                else
                {
                    // row doesn't exist, insert
                    var record = result.CreateRecord();
    
                    // set values
                    record.SetInt32(0, 1);
                    // etc. 
    
                    result.Insert(record);
                }
            }
        }
    } 
    

    0 讨论(0)
提交回复
热议问题