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">
--------------------------------------------------------------------------------
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">
相关文章推荐
- select records by Date in SQL Server
- ORA-00604: error occurred at recursive SQL level 1 ORA-01653: unable to extend table SYS.AUD$ by 8192 in tablespace SYSTEM
- ORA-00604: error occurred at recursive SQL level 1 ORA-01653: unable to extend table SYS.AUD$ by 8192 in tablespace SYSTEM
- java.sql.SQLException: ORA-01652: unable to extend temp segment by 128 in tablespace TEMP
- sql --it is not contained in either an aggregate function or the GROUP BY clause.
- How to Implement an Automatic Sliding Window in a Partitioned Table on SQL Server 2005
- An example to show how to make Tree structure data table in SQL server
- [Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL s
- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version
- ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your
- How do I sort an array of hashes by a value in the hash?
- Oracle index by table(Associative array) used in PostgreSQL
- 在使用Navicat for MySQL 出现 异常"You have an error in your SQL syntax......"
- IMP-00058: ORACLE error 30036 encountered ORA-30036: unable to extend segment by 8 in undo tablespace 'UNDOTBS1'
- Resetting an Associative array in PL/SQL?
- You have an error in your SQL syntax
- [优秀英文技术文章转载]Table Value Parameters in SQL Server 2008 and .NET (C#)
- ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your
- 报表工具FineReport报错:java.sql.SQLException: The database is already in use by another process: lockFile