在数据库表中添加int数据类型 [英] Adding int datatypes in a database table

查看:798
本文介绍了在数据库表中添加int数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





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



代码:



  int  AddAge; 
string getAge;

查询= SELECT * FROM TblEmp WHERE Id =' + id + 'AND Position =' + Manager + ';
theReader = conn.ExecuteStatement(Query);
while (theReader.Read())
getAge = theReader [ < span class =code-string>年龄]。ToString();
AddAge = int .Parse(getAge);


AddAge + getAge;
// 这是我被困的地方,我如何将年龄加起来并显示总数?

解决方案

让我们暂停并问自己一个问题,你如何找出一个人的年龄?他的出生日期,对。年龄不是恒定的,并且可以从每个人固定的出生日期推导出来。你应该在数据库表中存储出生日期而不是年龄。

要查找一个人的年龄,只需从表中检索他的出生日期,在后面的代码中进行计算。当前日期,您将始终获得正确的年龄。通过这种方式,您可以将代码中所有人的年龄相加并始终获得最新更新。


如果此查询仅用于一起添加的总年龄,则使用查询。否则使用另一个查询来获得总和



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





如有疑问,请参阅 w3School [ + ]; - )


Hi,

I have got a SQL database table in which i am querying from in my c# project. The table contains a column that takes in int as a datatype (Employers Age). I am querying from this column and I would like each and every record that is queried from this column to be added up and display the total of the records;

Code:

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?

解决方案

Let pause and ask yourself a question, how do you find out the age of a person? His date of birth, right. Age is not constant and is derivable from date of birth which is fixed for each individual. You should be storing date of birth, not age in the database table.
To find the age of a person, simply retrieve his date of birth from the table, do the calculation in code behind based on the current date and you will always get the correct age. In this way, you can sum the ages of all persons in code behind and always get the latest update.


If this query is used solely for the total age added together than use that in the query. Otherwise use another query to get only the sum

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



When in doubt see w3School[+] ;-)


这篇关于在数据库表中添加int数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆