Estaba desarrollando una pequeña aplicacion en Visual Studio 2005 que en algún futuro sera un Website. Mientras codificaba la capa de datos, cuando investigaba las propiedades del objeto SqlConnection, encontré la siguiente propiedad StatisticsEnabled. Como su nombre lo indica, si habilitamos esta propiedad en un objeto SqlConnection, se guardará un registro estadístico del trabajo sobre esa conexión. También podemos usar las funciones ResetStatistics y RetrieveStatistics para trabajar sobre estas estadísticas.

Personalmente yo utilizaría otros métodos para realizar estadísticas sobre la conexión, por ejemplo, utilizando contadores de rendimiento. Pero, como el dato me pareció mas que interesante, escribí una pequeña aplicación que muestre esos datos.

Aqui va el código:

Imports System.Data

Imports System.Data.SqlClient

Public Class frmCnnStatistics

    ‘ Conexiones

    Dim cnnString As String = "Data Source=.;Initial Catalog=Northwind;Integrated Security=True"

    Dim cnn As New SqlConnection(cnnString)

    Dim cmd As New SqlCommand()

    Dim da As New SqlDataAdapter

    Dim ds As New DataSet

    Private Sub btnGetData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetData.Click

        ‘ crea el comando

        cmd.CommandType = CommandType.Text

        cmd.CommandText = "SELECT * FROM CATEGORIES"

        cmd.Connection = cnn

        da.SelectCommand = cmd

        da.Fill(ds)

        ‘ muestra los datos

        DataGridView1.AutoGenerateColumns = True

        DataGridView1.DataSource = ds.Tables(0)

    End Sub

    Private Sub frmCnnStatistics_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

          ‘ habilita las estadisticas y abre la conexion

        cnn.StatisticsEnabled = True

        cnn.Open()

      End Sub

      Private Sub btnDisplayStatistics_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayStatistics.Click

        ‘ retrieve Dictionary containing statistics

        Dim htStats As Hashtable = cnn.RetrieveStatistics()

        Dim msg As String = ""

          ‘ display values in Label control

        For Each oKey As String In htStats.Keys

            msg &= "" & oKey & " = " & htStats(oKey) & vbCrLf

        Next

          MsgBox(msg)

      End Sub

End Class

Esta aplicación solo posee un par de botones y una grilla. Accede a la base de datos Northwind y obtiene todos los datos de la tabla Categories y a este set de datos lo asocia en una grilla. El otro boton, obtiene la coleccion de estadisticas de la conexion y la muestra.

Realizando una sola consulta contra la base de datos, el resultado de la muestra es el siguiente:

Key

Value

NetworkServerTime

0

BytesReceived

87390

UnpreparedExecs

1

SumResultSets

1

SelectCount

1

PreparedExecs

0

ConnectionTime

63446

ExecutionTime

150

Prepares

0

BuffersSent

1

SelectRows

8

ServerRoundtrips

1

CursorOpens

0

Transactions

0

BytesSent

56

BuffersReceived

11

IduRows

0

IduCount

0

Luego en la segunda ejecución los datos cambian un poco (remarcados) y se explican a ellos mismos. 😀

Sin emabrgo los mas significativos creo que son ExecutionTime y BytesReceived.

Key

Value

NetworkServerTime

0

BytesReceived

174780

UnpreparedExecs

2

SumResultSets

2

SelectCount

2

PreparedExecs

0

ConnectionTime

4506

ExecutionTime

110

Prepares

0

BuffersSent

2

SelectRows

16

ServerRoundtrips

2

CursorOpens

0

Transactions

0

BytesSent

112

BuffersReceived

22

IduRows

0

IduCount

0

Bueno espero que les sea útil.

Saludos

PD: También lo publiqué en El Guille.

 

4 responses to “Ado.Net 2.0 Connection Statistics”

Leave a comment

Discover more from El Bruno

Subscribe now to keep reading and get access to the full archive.

Continue reading