2013-11-23 21 views

回答

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; 
     } 
    } 
全局函數