为什么Sql Profiler将一些查询报告为SQL:BatchCompleted和一些作为RPC:已完成?

时间:2017-07-05 03:07:23

标签: c# sql-server ado.net

enter image description here

使用SELECT TOP 1 name FROM SiteMast...发出第一个查询SqlCommand.ExecuteReader

使用exec sp_executesql N'SELECT n.Nam...发出第三个查询SqlCommand.ExecuteReaderAsync,该查询应该相同。

我很确定async vs blocking不是这里的因素 - 在一个小样本上测试:

using System;
using System.Data.SqlClient;
using System.Threading.Tasks;

namespace sql
{
    class Program
    {
        static void Main(string[] args)
        {
            Sync();
            Async().GetAwaiter().GetResult();
        }

        private static void Sync()
        {
            using (var c = new SqlConnection("server=localhost;database=test;trusted_connection=true"))
            {
                c.Open();
                using (var cmd = c.CreateCommand())
                {
                    cmd.CommandText = "SELECT Namespace,ClientId FROM Client";
                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            Console.WriteLine($"{reader.GetString(0)}\t{reader.GetInt32(1)}");
                        }
                    }
                }
            }
        }
        private static async Task Async()
        {
            using (var c = new SqlConnection("server=localhost;database=dftest;uid=***;pwd=***"))
            {
                await c.OpenAsync();
                using (var cmd = c.CreateCommand())
                {
                    cmd.CommandText = "SELECT Namespace,ClientId FROM Client";
                    using (var reader = await cmd.ExecuteReaderAsync())
                    {
                        while (await reader.ReadAsync())
                        {
                            Console.WriteLine($"{reader.GetString(0)}\t{reader.GetInt32(1)}");
                        }
                    }
                }
            }
        }
    }
}

此代码生成两个SQL:BatchCompleted事件。

我的问题 - 是什么导致某些ADO.NET查询报告为SQL:BatchCompleted,有些报告为RPC:Completed

0 个答案:

没有答案