c# - 估计从 SQL 返回的数据集的大小

标签 c# sql-server dapper seq

我们有一个系统似乎在消耗大量数据,它使用 Dapper 进行数据库查询,使用 Seq 进行日志记录。我想知道除了使用 SQL Profiler 之外,是否有一种方法可以将日志记录添加到 Dapper 以记录以 MB 为单位返回的数据集的大小 - 这样我们就可以标记大型数据集以供审查?

这个问题has been asked a while ago但我想知道现在是否有一种无需 wireshark 且最好无需遍历行/单元格的方法?

最佳答案

我会配置 Provider Statistics for SQL Server用于基础存储库类中的连接。您可以添加一个配置设置来打开它,并轻松地将此信息保存到日志文件或任何您想要的地方。

来自 MSDN 的示例代码

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace CS_Stats_Console_GetValue
{
  class Program
  {
    static void Main(string[] args)
    {
      string connectionString = GetConnectionString();

      using (SqlConnection awConnection = 
        new SqlConnection(connectionString))
      {
        // StatisticsEnabled is False by default.
        // It must be set to True to start the 
        // statistic collection process.
        awConnection.StatisticsEnabled = true;

        string productSQL = "SELECT * FROM Production.Product";
        SqlDataAdapter productAdapter = 
          new SqlDataAdapter(productSQL, awConnection);

        DataSet awDataSet = new DataSet();

        awConnection.Open();

        productAdapter.Fill(awDataSet, "ProductTable");
        // Retrieve the current statistics as
        // a collection of values at this point
        // and time.
        IDictionary currentStatistics =
          awConnection.RetrieveStatistics();

        Console.WriteLine("Total Counters: " +
          currentStatistics.Count.ToString());
        Console.WriteLine();

        // Retrieve a few individual values
        // related to the previous command.
        long bytesReceived =
            (long) currentStatistics["BytesReceived"];
        long bytesSent =
            (long) currentStatistics["BytesSent"];
        long selectCount =
            (long) currentStatistics["SelectCount"];
        long selectRows =
            (long) currentStatistics["SelectRows"];

        Console.WriteLine("BytesReceived: " +
            bytesReceived.ToString());
        Console.WriteLine("BytesSent: " +
            bytesSent.ToString());
        Console.WriteLine("SelectCount: " +
            selectCount.ToString());
        Console.WriteLine("SelectRows: " +
            selectRows.ToString());

        Console.WriteLine();
        Console.WriteLine("Press any key to continue");
        Console.ReadLine();
      }

    }
    private static string GetConnectionString()
    {
      // To avoid storing the connection string in your code,
      // you can retrive it from a configuration file.
      return "Data Source=localhost;Integrated Security=SSPI;" + 
        "Initial Catalog=AdventureWorks";
    }
  }
}

关于c# - 估计从 SQL 返回的数据集的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38782117/

相关文章:

c# - 如何在不显式指定主键的情况下使用 Dapper Extensions 将对象插入 PostGreSql?

sql-server - 从 SQL Server 2008 中的 XML 字段中选择值

sql - 如何向使用 SELECT INTO 创建的表添加完整的主键

SQL Server INSERT 和 SELECT 语句之间出现死锁

c# - 使用 Dapper QueryMultiple 时在泛型参数中提供类型

c# - 如何传入将用作数据表中行的过滤器的 lambda?

c# - CLR 如何解释以下 LINQ 查询

c# - 如何在自定义保存操作 WFFM 中获取 Sitecore.Current.Site 对象?

c# - 如何更改不同构建配置的可执行文件名和图标?

c# - 创建数据库连接