Counting Records in a SQL Table by G.F. Weis GFW

Source: Internet
Author: User
Tags class generator count generator
Counting Records in a SQL Table by G.F. Weis GFW


--------------------------------------------------------------------------------

I am working on a project using my C # Class generator and realized I had no way to get a count of the number of rec Ords in the table. In the past I have used of the DataAdapter class-easy, but not very efficient.

I ran accross a post on one of the discussion forums and decided to build a new function in my class generator appropriate Ly named RecordCount. The function returns an ' int ' and the number of records in the table.

m_DBConnection is the Connection string to your database
Arepresentative is the name of our table
ID is unique column used in our table
Using the C # Class generator, the generated code is as follows:


public int RecordCount ()//Get Count of Records
{
int reccount=0;
String countcmd = "Select Count (Id) as total from arepresentative";
SqlConnection m_sqlconnection = new SqlConnection (m_DBConnection);
SqlCommand M_sqlcommand = new SqlCommand (Countcmd, m_sqlconnection);
Try
{
Open the Connection----------------------------
M_SqlCommand.Connection.Open ();
RecCount = (int) m_sqlcommand.executescalar ();
}//End Try
catch (Exception e)
{
throw new Exception ("Error in RecordCount ()->" + e.tostring ());
}
Finally
{
M_sqlconnection.close ();
M_sqlcommand.dispose ();
}
return reccount;
}//End Select





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.