Is it fast?

cathjonas

New Member
I have seen the code to stat all the record count,Is it the fastest way?<BR><BR><%@ Page Language="VB" %><BR><%@ Import Namespace="System.Data" %><BR><%@ Import Namespace="System.Data.SqlClient" %><BR><html><BR><script language="VB" runat="server"><BR> Sub Page_Load(sender As Object, e As EventArgs) <BR> Dim myDataSet As DataSet = new DataSet()<BR> Dim SelectCommand As String ="Select * from Publishers"<BR> Dim myConnection As SqlConnection = new SqlConnection("Data Source=test; User Id=test; Password=test; Initial Catalog=pubs")<BR> Dim myDataAdapter As SqlDataAdapter = new SqlDataAdapter(SelectCommand, myConnection)<BR> myDataAdapter.Fill(myDataSet, "Products")<BR> Dim RowCount As Integer = myDataSet.Tables("Products").Rows.Count<BR> countLabel.Text = RowCount.ToString()<BR> End Sub<BR></script><BR><asp:label id="countLabel" width=100 runat=Server/><BR></html><BR>When you Fill a dataAdapter, you're populating all of its rows with the rows returned from the database query. So, yes, once you've read in all that information, chceking the .Rows.Count property is lightning fast. Of course you're incurring the overhead of reading the entire's results into a DataSet, which is fine if you plan on working with that data.<BR><BR>However, if all you want is the number of records returned by a query, use a SELECT COUNT(*) FROM TableName WHERE WhereClause and use the .ExecuteScalar method of the proper Command object to get back the results.<BR><BR>hthand since we're talking speed as the issue here, I'd do SELECT COUNT(ID) FROM .... since your ID field would most likely be indexed, and depending on your table size would return your count more quickly
 
Back
Top