注:This method gets the record count much faster than select count(*) from table_name
1、一般情况下,可以通过聚合函数Count实现查询出记录的总数
select count(*) as 记录总数 from table_name
2、也可以通过查询系统表sysindexes实现
select rows from sysindexes where id=object_id('表名') and indid<2
语法:Object_id('object')
返回类型:int
'object'是要使用的对象,其数据类型是char或nchar
若要指定一个临时表名,则必须在临时表名前面加上数据库名。如:select rows from sysindexes where object_id('tempdb..#table')
3、2查询的速度要比1查询的速度快,因此读取数据时可以不用DataReader
例:
private int getrecords()
{
int intcount = 0;
string str_sql = "select rows from sysindexes where id=object_id('数据库.表名') and indid<2";
SqlCommand cmd = new SqlCommand(str_sql, Conn);
intcount = (int)cmd.ExecuteScalar();
cmd.Dispose();
return intcount;
}
{
int intcount = 0;
string str_sql = "select rows from sysindexes where id=object_id('数据库.表名') and indid<2";
SqlCommand cmd = new SqlCommand(str_sql, Conn);
intcount = (int)cmd.ExecuteScalar();
cmd.Dispose();
return intcount;
}