我知道你已经标记了 sql 和 sql-server-ce 但如果你愿意使用 c# 代码来解决这个问题..:
使用 c# 和结果集,这是我使用 SQL CE 为我的移动应用程序所做的:
// UpdateRow is a struct/class to hold the data for each row
// SqlCeConn = connection string for db
SqlCeCommand cmd = new SqlCeCommand("Table1", SqlCeConn);
cmd.CommandType = CommandType.TableDirect;
cmd.IndexName = "Column1";
using (SqlCeResultSet rsltSet = cmd.ExecuteResultSet(ResultSetOptions.Scrollable | ResultSetOptions.Updatable))
{
if (UpdateRow.Column1> 0) // or != "" if it's string etc
{
if (rsltSet.Seek(DbSeekOptions.FirstEqual, UpdateRow.Column1))
FoundRecord = true;
}
rsltSet.Read();
if (FoundRecord)
{
// Update
rsltSet.SetInt32(1, UpdateRow.Column1);
rsltSet.SetInt32(2, UpdateRow.Column2);
// etc
rsltSet.Update();
}
else
{
// Insert new record
SqlCeUpdatableRecord record = rsltSet.CreateRecord();
record.SetInt32(0, UpdateRow.Column1);
record.SetInt32(1, UpdateRow.Column2);
// etc
rsltSet.Insert(record, DbInsertOptions.PositionOnInsertedRow);
}
}
cmd.Dispose();
}
catch (Exception e)
{
// Deal with exception
}