Counting Records in an SQL Table by G.F. Weis Gfw

<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
an class="baidu_ad"> an> ank>Countank>ing ank>Records ank>in an  SQL Table by G.F. Weis Gfw


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

I was workank>ing on a project usank>ing my C# Class Generator  and realized that I had no way to get a count of the number of ank>Records ank>in the Table. ank>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 ank>in my class generator appropriately named RecordCount. The function returns an 'ank>int' with the number of ank>Records ank>in the Table.

m_dbConnection is the Connection strank>ing to your database
aRepresentative is the name of our Table
Id is unique column used ank>in our Table
Usank>ing the C# Class Generator, the generated code is as follows:


public ank>int RecordCount() // Get Count of ank>Records
{
    ank>int recCount=0;
    strank>ing 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 = (ank>int)m_SQLCommand.ExecuteScalar();
    } // end try
    catch (Exception e)
    {
        throw new Exception("Error ank>in RecordCount() -> " + e.ToStrank>ing());
    }
    fank>inally
    {
        m_SQLConnection.Close();
        m_SQLCommand.Dispose();
    }
    return recCount;
} // end Select

11AIc16010S036.jpg
angzhanyunyank>ing/wangzhanyouhua/" title="seo,搜索引擎优化">seover="wank>indow.status='正文--ank>Countank>ing ank>Records ank>in an SQL Table by G.F. Weis Gfw';return true">
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
阅读更多
个人分类: .net
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭