使用SQLCeResultSet更新/插入表
我有一个SQL Compact Edition数据库,我定期更新(通过Web服务)。
我写入数据库的部分花了太长时间。 我目前正在使用Linq to Datasets(如本问题所示 )。 我听说如果我使用SQLCeResultSet这样做会更快。
所以,鉴于我有一个这样的表:
tblClient + - CLIENT_ID {唯一标识符}(主键) + - CLIENT_NAME {varchar(100)} + - CLIENT_ACTIVE {bit}
我从我的Web服务获得的对象看起来像这样:
class Client { public Guid ClientID { get; set; } public String ClientName { get; set; } public bool Active { get; set; } }
如何将100个Client对象导入数据库?
更新现有行并插入数据库中尚未存在的行(由主键确定)?
任何示例代码都会很棒。 我有一个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); } } } }