如何統計c#中sql表的行數? 我需要從我的數據庫中提取一些數據...如何計算從c#中的sql表的行數?
6
A
回答
3
你的意思是嗎?
SELECT COUNT(*)
FROM yourTable
WHERE ....
27
你可以試試這樣:
select count(*) from tablename where columname = 'values'
C#代碼將是這樣的: -
public int A()
{
string stmt = "SELECT COUNT(*) FROM dbo.tablename";
int count = 0;
using(SqlConnection thisConnection = new SqlConnection("Data Source=DATASOURCE"))
{
using(SqlCommand cmdCount = new SqlCommand(stmt, thisConnection))
{
thisConnection.Open();
count = (int)cmdCount.ExecuteScalar();
}
}
return count;
}
4
你需要從C#中的數據庫連接第一。然後,你需要傳遞下面的查詢作爲commandText。
Select count(*) from TableName
使用的ExecuteScalar /的ExecuteReader來獲取返回的計數。
1
可以使您可以使用所有的時間作爲
public static int GetTableCount(string tablename, string connStr = null)
{
string stmt = string.Format("SELECT COUNT(*) FROM {0}", tablename);
if (String.IsNullOrEmpty(connStr))
connStr = ConnectionString;
int count = 0;
try
{
using (SqlConnection thisConnection = new SqlConnection(connStr))
{
using (SqlCommand cmdCount = new SqlCommand(stmt, thisConnection))
{
thisConnection.Open();
count = (int)cmdCount.ExecuteScalar();
}
}
return count;
}
catch (Exception ex)
{
VDBLogger.LogError(ex);
return 0;
}
}
全局函數
相關問題
- 1. 如何計算SQL中的2個表?
- 2. 如何從兩個表中的SQL語言計算SQL數量列的總和
- 3. 計算SQL數據庫中的行數
- 4. 如何計算sql中的零個數?
- 5. 如何計算html文件中的表中的行C#
- 6. SQL表中的計算,從臨時表中的總行值函數
- 7. 如何計算從wordpress使用sql查詢返回的行數?
- 8. SQL - 如何進行計算
- 9. SQL:從第3表計算行
- 10. 如何計算行數在SQL表中使用SqlClient類
- 11. 計算SQL Server中的行數
- 12. 如何計算C中的浮點數?
- 13. JS如何計算表中的行值?
- 14. 在PHP中計算SQL表的數量?
- 15. 如何計算插入SQL Server表中的值的數量?
- 16. 如何計算sql中的兩個表的數據比例
- 17. SQL Server報表服務:如何插入計算的「中間」行
- 18. 如何從列表中計算總計
- 19. 如何計算行列C數組?
- 20. C#計數sql結果中的行數
- 21. 從同一表中的行計算SQL存儲過程中的總數
- 22. SQL如何計算數字
- 23. SQL:如何計算總計列的行數?
- 24. SQL:如何計算有限的行並重置計數器
- 25. SQL Server如何計算估計的行數?
- 26. 如何在SQL Server中進行計算?
- 27. 如何計算使用C++的文件中的行數?
- 28. 計算文件中的行數 - C
- 29. 如何從另外兩個表中動態計算表中的行數?
- 30. Sql Server - 如何計算數據庫中每個表的一些行的大小?