gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:41:43 27 4
gpt4 key购买 nike

我们有一个系统似乎在消耗大量数据,它使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com