I want to find the number of rows in a table from C#.
Please give me some g开发者_如何学Cuide. Thanks.
you can try this query:
SELECT COUNT(*) AS Length FROM TableName
This will not necessarily be accurate due to transactions that may be in flight (the same issue that can occur if you use less aggressive isolation levels / NOLOCK).
SELECT [RowCount] = SUM(row_count)
FROM sys.dm_db_partition_stats
WHERE [object_id] = OBJECT_ID('dbo.tablename')
AND index_id IN (0,1);
This will be much faster than SELECT COUNT(*)
but it depends on your goal - do you want 100% accuracy at the cost of concurrency, or do you want a ballpark?
To get the number of rows, you can simply use the COUNT function:
SELECT COUNT(*) Length
FROM SomeTable
Well, the obvious way:
SELECT COUNT(*) FROM yourTable;
I'll let Aaron or someone else post the fast way to do it, then we'll have the accuracy discussion/clarification... :-D
精彩评论