简体   繁体   English

事务性sqlite? 在C#中

[英]transactional sqlite? in C#

I am writing a prototype application. 我正在写一个原型应用程序。 Right now some things fail such as inserting a non unique column. 现在有些事情失败了,比如插入一个非唯一的列。 In this case i would like to rollback everything i have did. 在这种情况下,我想回滚我所做的一切。 How do i do that? 我怎么做? I also notice in sqlite i need to commit the data, in C# it seems to do it automatically which makes me suspect there is an automatic rollback option? 我还注意到在sqlite中我需要提交数据,在C#中它似乎自动执行它会让我怀疑有自动回滚选项吗?

Rollback: It looks like what you are looking for is the command text "INSERT OR ROLLBACK ... " 回滚:看起来你正在寻找的是命令文本“INSERT OR ROLLBACK ...”

Transaction: sqlite automatically puts each command into its own transaction unless you specifically state when to begin. 事务:除非您明确说明何时开始,否则sqlite会自动将每个命令放入其自己的事务中。 EDIT: TML explains this portion a bit more in-depth in his answer 编辑:TML在他的回答中更深入地解释了这一部分

example of explicitly opening/committing a transaction is: 显式打开/提交事务的示例是:

using (DbTransaction dbTrans = myDBConnection.BeginTransaction())
{
     using (DbCommand cmd = myDBConnection.CreateCommand())
     {
         ...
     }
     dbTrans.Commit();
}

SQLite.org says: SQLite.org说:

The changes to locking and concurrency control in SQLite version 3 also introduce some subtle changes in the way transactions work at the SQL language level. SQLite版本3中对锁定和并发控制的更改也在事务处理SQL语言级别的方式中引入了一些细微的变化。 By default, SQLite version 3 operates in autocommit mode. 默认情况下,SQLite版本3以自动提交模式运行。 In autocommit mode, all changes to the database are committed as soon as all operations associated with the current database connection complete. 在自动提交模式下,只要与当前数据库连接关联的所有操作都完成,就会提交对数据库的所有更改。

The SQL command "BEGIN TRANSACTION" (the TRANSACTION keyword is optional) is used to take SQLite out of autocommit mode. SQL命令“BEGIN TRANSACTION”(TRANSACTION关键字是可选的)用于使SQLite脱离自动提交模式。 Note that the BEGIN command does not acquire any locks on the database. 请注意,BEGIN命令不会获取数据库上的任何锁定。 After a BEGIN command, a SHARED lock will be acquired when the first SELECT statement is executed. 在BEGIN命令之后,执行第一个SELECT语句时将获取SHARED锁。 A RESERVED lock will be acquired when the first INSERT, UPDATE, or DELETE statement is executed. 执行第一个INSERT,UPDATE或DELETE语句时将获取RESERVED锁。 No EXCLUSIVE lock is acquired until either the memory cache fills up and must be spilled to disk or until the transaction commits. 在内存缓存填满并且必须溢出到磁盘或事务提交之前,不会获取EXCLUSIVE锁。 In this way, the system delays blocking read access to the file file until the last possible moment. 通过这种方式,系统延迟阻止对文件文件的读访问,直到最后一刻。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM