在提交之前从事务中获取SQL

时间:2018-05-09 18:16:53

标签: c# sql-server database transactions

我正在使用SQL Server数据库在C#中开发一个项目。

当查询有任何条件时,我想在数据库中保存一些信息,所以我的想法是有一个通用的方法来启动事务的提交,如果提交具有所需的条件,则保存信息。

我的问题:有没有办法在.commit()之前获取交易的SQL代码?

例如,类似的东西:

public void insertInfo(object)
{
     using (Context context = new Context())
     {
         [Do an insert]
         [Do a second insert]
         [Do an update]     

         context.Save()
     }
} 

// In the Context
public void Save()
{
    [Here I want to get the SQL statement(s) that will be affected by the "transaction.Commit()"]
   transaction.Commit();
}

1 个答案:

答案 0 :(得分:2)

没有内置命令或函数可以获取事务的当前代码。

您可以通过创建字符串变量自己构建它,每次在函数中执行命令时都将其添加到变量中,然后在提交事务时读取变量。

相关问题