Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.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# 正在检索存储过程的统计信息,SelectRows始终为0_C#_Sql_Sql Server_Powershell - Fatal编程技术网

C# 正在检索存储过程的统计信息,SelectRows始终为0

C# 正在检索存储过程的统计信息,SelectRows始终为0,c#,sql,sql-server,powershell,C#,Sql,Sql Server,Powershell,我试图收集一些SQL查询的统计数据。 我正在使用SqlDbConnection类的RetrieveStatistics()方法获取统计信息,并使用SqlCommand的ExecuteReader()方法运行查询。 RetrieveStatistics()方法返回字典,字典中填充了所执行查询的统计信息。 当我运行常规查询时,SelectRowsproperty of dictionary包含查询返回的实际行数。但当我运行存储过程时,SelectRows始终为零,尽管读卡器肯定包含行 我在每次查询之

我试图收集一些SQL查询的统计数据。 我正在使用SqlDbConnection类的RetrieveStatistics()方法获取统计信息,并使用SqlCommand的ExecuteReader()方法运行查询。 RetrieveStatistics()方法返回字典,字典中填充了所执行查询的统计信息。 当我运行常规查询时,SelectRowsproperty of dictionary包含查询返回的实际行数。但当我运行存储过程时,SelectRows始终为零,尽管读卡器肯定包含行

我在每次查询之前调用ResetStatistics(),并且StatisticsEnabled设置为true

这是我的Powershell代码:

### Stored procedure

$cn = New-Object system.data.sqlclient.sqlconnection
$cn.ConnectionString = "Data Source=localhost;Initial Catalog=XXXX;Integrated Security=SSPI"
$cn.StatisticsEnabled = $true
$cmd = $cn.CreateCommand()
$cmd.CommandText = "[dbo].[spGetXXXX]"
$cmd.CommandType = "StoredProcedure"
$cn.Open()
$cmd.ExecuteReader()
# several rows returned
$cn.RetrieveStatistics()

Name                           Value
----                           -----
BytesReceived                  300
SumResultSets                  1
ExecutionTime                  5
Transactions                   0
BuffersReceived                1
IduRows                        0
ServerRoundtrips               1
PreparedExecs                  0
BytesSent                      132
SelectCount                    1
CursorOpens                    0
ConnectionTime                 51299
Prepares                       0
SelectRows                     0
UnpreparedExecs                1
NetworkServerTime              3
BuffersSent                    1
IduCount                       0

### Regular SQL query
$cn2 = New-Object system.data.sqlclient.sqlconnection
$cn2.ConnectionString = "Data Source=localhost;Initial Catalog=XXXX;Integrated Security=SSPI"
$cn2.StatisticsEnabled = $true
$cmd2 = $cn2.CreateCommand()
$cmd2.CommandText = "SELECT * FROM XXXX"
$cn2.Open()
$cmd2.ExecuteReader()

#rows returned

$cn2.RetrieveStatistics()

Name                           Value
----                           -----
BytesReceived                  12357
SumResultSets                  1
ExecutionTime                  12
Transactions                   0
BuffersReceived                2
IduRows                        0
ServerRoundtrips               1
PreparedExecs                  0
BytesSent                      98
SelectCount                    1
CursorOpens                    0
ConnectionTime                 11407
Prepares                       0
SelectRows                     112
UnpreparedExecs                1
NetworkServerTime              0
BuffersSent                    1
IduCount                       0

这两个查询之间的区别在于,在存储过程调用读取器保持打开状态之后,行号统计信息不会更新

因此,正确的代码如下所示:

$connectionn.ResetStatistics()
$reader = $command.ExecuteReader()
$reader.Close() # !
$connection.RetrieveStatistics()