jueves, mayo 05, 2005

Codigo ASP.Net - Contar registros distintos de una tabla

Use como siempre el wizard de Web Matrix para realizar un select. Modifique la cadena de consulta para contar los disitintos registros de la tabla y en lugar de generar un dataset o datareader la funcion genera un integer:


Function cuentaPC(ByVal iDSubasta As Integer) As Integer
Dim connectionString As String = System.Configuration.ConfigurationSettings.AppSettings("Mi_CONNECTION_STRING")
Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString)

Dim queryString As String = "SELECT Count([Paletas].[IDPaleta]) as CuentaPaletas FROM (SELECT DISTINCT [Paletas].[IDPaleta] FROM [Paletas], [Lotes] WHERE (([Paletas].[IDPaleta] "& _
"= [Lotes].[IDPaleta]) AND ([Paletas].[IDSubasta] = @IDSubasta)))"
Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection


Dim dbParam_iDSubasta As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
dbParam_iDSubasta.ParameterName = "@IDSubasta"
dbParam_iDSubasta.Value = iDSubasta
dbParam_iDSubasta.DbType = System.Data.DbType.Int32
dbCommand.Parameters.Add(dbParam_iDSubasta)

dbConnection.Open
Dim intCuentaPaletas as Integer = dbCommand.ExecuteScalar()

Return intCuentaPaletas
End Function


Listo!

No hay comentarios.: