使用 SQLCeResultSet 更新/插入表

2024-04-10

我有一个定期更新的 SQL Compact Edition 数据库(通过 Web 服务)。

我写入数据库的部分花费的时间太长。我目前正在使用 Linq to Datasets 进行此操作(如这个问题 https://stackoverflow.com/questions/2419125/slow-update-insert-into-sql-server-ce-using-linqtodatasets)。我有heard https://stackoverflow.com/questions/2419125/slow-update-insert-into-sql-server-ce-using-linqtodatasets/2438870#2438870如果我使用 SQLCeResultSet 来完成它,它会工作得更快。

所以,考虑到我有一个这样的表:



tblClient
   +- CLIENT_ID      {Unique identifier} (Primary Key)
   +- CLIENT_NAME    {varchar (100)}
   +- CLIENT_ACTIVE  {bit}  

我从网络服务中获得的对象如下所示:

class Client
{
   public Guid ClientID { get; set; }
   public String ClientName { get; set; }
   public bool Active { get; set; }
}

如何将 100 个 Client 对象存入数据库?

Updating现有行并插入数据库中尚未存在的行(由主键确定)?

任何示例代码都会很棒。我有一个SqlCeConnection, 但没有别的。

谢谢你的帮助!


它看起来像这样:

(编辑插入或更新)

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);
            }
        }
    }
} 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 SQLCeResultSet 更新/插入表 的相关文章

随机推荐