Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# asp.net在用户访问数据库时,在数据库中存储分数总和_C#_Asp.net_Mysql - Fatal编程技术网

C# asp.net在用户访问数据库时,在数据库中存储分数总和

C# asp.net在用户访问数据库时,在数据库中存储分数总和,c#,asp.net,mysql,C#,Asp.net,Mysql,我在我的aspx页面中得到以下sql查询,以更新phpmyadmin中的数据库。我的软件是VisualWebDeveloper2010 cmd = connection.CreateCommand(); cmd.CommandText = "UPDATE (userscore) SET score = @score WHERE username = @username"; cmd.Parameters.AddWithValue("@userna

我在我的aspx页面中得到以下sql查询,以更新phpmyadmin中的数据库。我的软件是VisualWebDeveloper2010

  cmd = connection.CreateCommand();
            cmd.CommandText = "UPDATE (userscore) SET score = @score WHERE username = @username";
            cmd.Parameters.AddWithValue("@username", username);
            cmd.Parameters.AddWithValue("@score", userscore);
            var numRowsAffected = cmd.ExecuteNonQuery();
            if (numRowsAffected == 0)
            {
                cmd.CommandText = "INSERT INTO userscore (username, score)VALUES(@username,   @score)";
                cmd.ExecuteNonQuery();
            }
当特定用户名尚未在数据库中时,此查询会在数据库中添加用户名加is分数。这就是我想要的。但是,例如,当用户名admin在数据库中的分数为-1(在我的应用程序中,您可以投票+1或-1)并且他获得另一个投票+1时,我想将这些值的总和(在本例中为-1+1=0)存储在数据库中作为新分数,并删除旧值,在本例中为-1。

我只需要将您的“upsert”包装起来编码到存储过程中,并传递userid和分数递增/递减

您当前的代码不会更新分数,只会用传入的内容覆盖分数

create proc SetUserScore
@UserName nvarchar(100),
@ScoreChange int
AS
BEGIN
    IF EXISTS(SELECT 1 FROM userscore WHERE username = @UserName)
    BEGIN
      UPDATE userscore 
      SET score = score + @ScoreChange
      WHERE username = @UserName
    END
    ELSE
    BEGIN
      INSERT INTO userscore (username, score)VALUES(@username,   @score)
    END

END

Thnx的回答,但我必须把这个存储过程放在我的aspx文件的哪里?当我在mysql命令行中键入这个命令时,它会说:错误1046(3D000):没有选择数据库?我不知道你在使用mysql,请将你的问题标记为这样。我的代码将在SQL Server中创建一个存储过程;不知道你是如何在mysqlgooglecan中做到这一点的!