2013-11-23 4 views

答えて

3

あなたはこれが好きですか?

SELECT COUNT(*) 
FROM yourTable 
WHERE .... 
27

あなたはこのようにしてみてください可能性があります

select count(*) from tablename where columname = 'values' 

C#のコードは次のようなものになります - あなたが最初の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

。次に、queryの下に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; 
     } 
    } 
として、すべての時間を使用することができ、グローバル関数を作ることができます
関連する問題