欢迎光临
我们一直在努力

Counting Records in an SQL Table by G.F. Weis Gfw-.NET教程,数据库应用

建站超值云服务器,限时71元/月

counting records in an  sql table by g.f. weis gfw

——————————————————————————–

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

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » Counting Records in an SQL Table by G.F. Weis Gfw-.NET教程,数据库应用
分享到: 更多 (0)

相关推荐

  • 暂无文章