2013-11-23 11 views

cevap

3

Bunun gibi mi?

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

Böyle deneyebilirsiniz:

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

C# kodu böyle bir şey olacak: - Önce C# bir veritabanı bağlantısı yapmak gerekir

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

. Ardından, aşağıdaki sorguyu commandText olarak geçirmeniz gerekir.

Select count(*) from TableName

Kullanım ExecuteScalar/ExecuteReader döndü sayım sonucuna varmak için.

1

Sen

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; 
     } 
    } 
olarak her zaman kullanabilirsiniz küresel işlev yapabilir
İlgili konular