i was working on a project using my c# class generator and realized that i had no way to get a count of the number of records in the table. in the past i have used 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 appropriately named recordcount. the function returns an 'int' with 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