简体   繁体   English

如何在sql c中增加一个整数#

[英]how to increment an integer in sql c#

I am making a voting system. 我正在制作投票系统。 I have a problem adding a count(increment each time the candidate is voted) to each candidate that they voted. 我在向他们投票的每个候选人添加一个计数(每次候选人投票时增加)时遇到问题。

My Vote Button code is : 我的投票按钮代码是:

    sc.Open();
        try
        {
            cmd = new SqlCommand("UPDATE TableVote SET Vcount=Vcount+1 WHERE id=@count", sc);

            cmd.Parameters.AddWithValue("@count", TxtPresID.Text);

            int res = cmd.ExecuteNonQuery();
            if (res > 0)
            {
                MessageBox.Show(TxtPresID.Text.ToString() + " Saved!");
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            sc.Close();
        }

and my table is 我的桌子是

我的sql

the result of Vcount is just NULL .. Vcount的结果只是NULL ..

空值

What am i doing wrong? 我究竟做错了什么? or is my Sql statement correct? 或者我的Sql语句是否正确?

Looks like your Vcount is NULL . 看起来你的VcountNULL As others have pointed out, you should make that column default to 0 or you could do this 正如其他人指出的那样,您应该将该列默认设置为0否则您可以执行此操作

UPDATE TableVote SET Vcount=ISNULL(Vcount, 0) + 1 WHERE id=@count

Set the default Value of Vcount to be 0. It shouldn't be a null column. 将默认值Vcount设置为0.它不应该是空列。

Here is the query 这是查询

ALTER TABLE {TABLENAME} 
ADD {COLUMNNAME} {TYPE} {NULL|NOT NULL} 
CONSTRAINT {CONSTRAINT_NAME} DEFAULT {DEFAULT_VALUE}

ALTER TABLE TableVote 
ADD Vcount int NOT NULL 
CONSTRAINT  tablevote_vcount_default DEFAULT 0

Another option is to check, whether Vcount is NULL 另一种选择是检查Vcount是否为NULL

UPDATE TableVote SET Vcount=ISNULL(Vcount, 0) + 1 WHERE id=@count

You shouldn't be updating the count row every time. 您不应每次都更新计数行。 if you have 2 votes incoming at the same time, it might have unpredictable consequences. 如果您同时收到2张投票,则可能会产生不可预测的后果。

A better way would be to have a table "votes" and then insert a row for every vote, including the candidate ID, the voter ID and the date of vote. 更好的方法是让表格“投票”,然后为每次投票插入一行,包括候选人ID,选民ID和投票日期。 also means you can have historical data for different voters, and that you can easily change votes if needed. 也意味着您可以拥有不同选民的历史数据,并且您可以根据需要轻松更改投票。

Learn that 了解这一点

Null + 1 = Null

So your Vcount will always be null when adding 1. 因此,添加1时,您的Vcount将始终为null。
Vcount must be not nullable, and it's default value muste be 0. Then when you will add 1 to 0, it will give you the good result. Vcount必须不可为空,并且它的默认值muste为0.然后当你将1加0时,它会给你带来好的结果。

You can also Change your SQL Statement so that zero is used if the VCOUNT-Column is null: 您还可以更改SQL语句,以便在VCOUNT-Column为空时使用零:

"UPDATE TableVote SET Vcount=ISNULL(Vcount, 0)+1 WHERE id=@count"

This is especially useful if you cannot change the database schema to be Not-Null and have a Default value. 如果您无法将数据库模式更改为非空并且具有默认值,则此功能尤其有用。

Vcount必须具有默认的0值,而Not Null,cause是一个Int Field。

If i am not wrong why not just use your primary key for it , 如果我没有错,为什么不使用你的主键,

UPDATE TableVote SET Vcount= max(id)  WHERE id=@count

OR 要么

If some id's are deleted and you don't want to keep records or count deleted votes then 如果某些ID被删除,您不想保留记录或计算删除的投票

  UPDATE TableVote SET Vcount= COUNT(id)  WHERE id=@count

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM