c#
2014-03-31 39 views 0 likes 
0

我有一個SQL數據庫表,我從中查詢我的c#項目。該表包含一個列,它將int作爲數據類型(僱主年齡)。我正在查詢這一列,並希望將從該列查詢的每條記錄加起來並顯示記錄的總數;從數據庫表中添加數據類型

代碼:

int AddAge; 
string getAge; 

    Query = "SELECT * FROM TblEmp WHERE Id = '" + id + "' AND Position = '"+Manager+"'"; 
         theReader = conn.ExecuteStatement(Query); 
         while(theReader.Read()) 
         getAge = theReader["Age"].ToString(); 
         AddAge = int.Parse(getAge); 


        AddAge + getAge; // this is where i am stuck, How can I add up the ages and display the the total? 

回答

0

我想你只需要添加括號內

while(theReader.Read()) 
{ 
         getAge = theReader["Age"].ToString(); 
         AddAge = int.Parse(getAge); 


        AddAge + getAge; 
} 

如果你只需要時代的總和,你可以改變你的SQL語句代碼

Query = "SELECT SUM(Age) FROM TblEmp WHERE Id = '" + id + "' AND Position = '"+Manager+"'"; 

then execute reader.executescalar

相關問題