Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用C#的ado.net为什么SqlTransaction很快?_C#_Asp.net_Ado.net_Sqltransaction - Fatal编程技术网

使用C#的ado.net为什么SqlTransaction很快?

使用C#的ado.net为什么SqlTransaction很快?,c#,asp.net,ado.net,sqltransaction,C#,Asp.net,Ado.net,Sqltransaction,我正在从事一个asp.net web应用程序项目 我尝试了使用SqlTransaction和不使用SqlTransaction的相同操作。使用SqlTransaction的代码的执行时间比不使用SqlTransaction的代码快 我想知道背后的原因,请让我知道SqlTransaction是如何影响性能的 这两个代码对和withput SqlTransaction执行相同的操作 using (SqlConnection connection = new SqlConnection(connect

我正在从事一个asp.net web应用程序项目

我尝试了使用SqlTransaction和不使用SqlTransaction的相同操作。使用SqlTransaction的代码的执行时间比不使用SqlTransaction的代码快

我想知道背后的原因,请让我知道SqlTransaction是如何影响性能的

这两个代码对和withput SqlTransaction执行相同的操作

using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();

    SqlCommand command = connection.CreateCommand();
    SqlTransaction transaction;

    // Start a local transaction.
    transaction = connection.BeginTransaction("SampleTransaction");

    // Must assign both transaction object and connection 
    // to Command object for a pending local transaction
    command.Connection = connection;
    command.Transaction = transaction;

    try
    {
        command.CommandText =
            "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
        command.ExecuteNonQuery();
        command.CommandText =
            "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
        command.ExecuteNonQuery();

        // Attempt to commit the transaction.
        transaction.Commit();
        Console.WriteLine("Both records are written to database.");
    }
    catch (Exception ex)
    {
        Console.WriteLine("Commit Exception Type: {0}", ex.GetType());
        Console.WriteLine("  Message: {0}", ex.Message);

        // Attempt to roll back the transaction. 
        try
        {
            transaction.Rollback();
        }
        catch (Exception ex2)
        {
            // This catch block will handle any errors that may have occurred 
            // on the server that would cause the rollback to fail, such as 
            // a closed connection.
            Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
            Console.WriteLine("  Message: {0}", ex2.Message);
        }
    }
}

请发布你的代码,我会说这取决于你的测试方法。仅运行同一个操作(不带事务)一次并不能告诉您多少实际性能。在同一事务中,我调用存储过程500次。使用ADO.Net中的事务从内存中调用,只需在执行代码之前向数据库发出“begin transaction”调用,因此,如果它加速了事情的发展,我会感到惊讶。我怀疑您的DB在没有事务的情况下进行测试时承受了更大的压力,或者您在如何对其进行基准测试时犯了错误。我对其进行了多次测试。