您的位置:首页 > 数据库

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

2008-05-01 03:16 597 查看
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



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">
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐