f#关键字使用和使用

use*_*321 15 f#

我正在尝试使用System.Data.SqliteF#.在C#中,我有类似的代码

 using (DbTransaction dbTrans = con.BeginTransaction()) {
     using (SQLiteCommand cmd = con.CreateCommand()) {
         //blahblah
     }
     dbTrans.Commit();
 }
Run Code Online (Sandbox Code Playgroud)

但是在F#中,当我使用using上面类似的两个时,我得到错误the type bool is not compatible with the type IDisposable... 编辑我很抱歉我的问题.use适用于F#案例.我只是不知道如何关闭/删除我的quesetions.

Tom*_*cek 16

要添加一些细节 - 如果需要显式标记命令有效的范围(为了获得与C#示例完全相同的行为,在cmd调用之前处置id Commit),您可以编写:

use dbTrans = con.BeginTransaction()
( use cmd = con.CreateCommand()
  cmd.BlahBlahBlah() )
dbTrans.Commit()
Run Code Online (Sandbox Code Playgroud)

范围只是定义符号的表达式的一部分,因此您可以使用括号将其显式化.

using只是一个F#函数,您可以在use添加特殊语法之前使用它.仅供参考,语法如下:

using (con.BeginTransaction()) (fun dbTrans ->
  using (con.CreateCommand()) (fun cmd ->
    cmd.BlahBlahBlah() )
dbTrans.Commit() )
Run Code Online (Sandbox Code Playgroud)

使用代码编写use肯定是一个更好的主意(但你可以定义你的函数,比如using封装更有趣的行为 - 例如事务).


Ale*_*lex 9

在f#中它是

use dbTrans = new con.BeginTransaction ()
Run Code Online (Sandbox Code Playgroud)

use cmd = con.CreateCommand()
Run Code Online (Sandbox Code Playgroud)

这些将在你的功能结束时处理