从数据库表中添加数据类型

时间:2014-03-31 13:34:44

标签: c#

我有一个SQL数据库表,我在c#项目中查询。该表包含一个以int作为数据类型(Employers Age)的列。我正在查询此专栏,我希望将此列中查询的每条记录添加起来并显示记录总数;

代码:

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?

1 个答案:

答案 0 :(得分: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+"'";

然后execute reader.executescalar