mario.deghetto

Visual Basic & .NET
posts - 332, comments - 79, trackbacks - 0

#30: Conteggio record con ADO.NET

L’articolo http://support.microsoft.com/kb/308050/en-us spiega che non è possibile conoscere in anticipo il numero di record restituiti dalla query con un DataReader, perché per conoscere il numero di
record è necessario spostarsi all’ultimo record. In quest’altro articolo viene sostanzialmente spiegata la stessa cosa: http://www.velocityreviews.com/forums/t112093-datareader-and-recordcount.html.

Per risolvere questo problema è possibile eseguire un conteggio PRIMA di aprire il DataReader, con istruzioni simili alle seguenti:

VB:

Dim cmd As SqlCommand = _
   New SqlCommand("SELECT COUNT(*) FROM Clienti;", conn)
Dim numeroClienti As Integer = CInt(cmd.ExecuteScalar())

C#:

SqlCommand cmd = _
   new SqlCommand("SELECT Count(*) FROM Clienti", conn);
Int32 count = (Int32)cmd.ExecuteScalar;

Print | posted on venerdì 15 gennaio 2010 5.32 |

Feedback

Gravatar

# re: #30: Conteggio record con ADO.NET

In VB non riesco ad utilizzare il suggerimento per il conteggio dei records.

07/02/2010 20.33 | Alex
Gravatar

# re: #30: Conteggio record con ADO.NET

ma ho risolto così

Dim stringaConnessione As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Archivio.mdb"

' apertura connessione
Dim Cn As New Data.OleDb.OleDbConnection(stringaConnessione)
Cn.Open()
' preparazione ed esecuzione della richiesta
Dim Conta As String = "SELECT count (*) FROM Anagrafica"

Dim comando As New Data.OleDb.OleDbCommand(Conta, Cn)
Dim Numero As Integer = comando.ExecuteScalar()
' chiusura connessione
Cn.Close()

LblConta.Text = Numero
07/02/2010 21.38 | Alex

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 8 and 3 and type the answer here:

Powered by: