变量名'@ newhealth'已经被声明。 [英] The variable name '@newhealth' has already been declared.
问题描述
这是我在用户攻击时调用的函数。我正在尝试更新敌人的健康状况,并表示@newhealth已经宣布。 除了UPDATE,我还应该使用其他东西吗? 我也试过使用AddWithValue("@ newhealth",newEnemyHealth)
并说明了同样的事情。
if(userAtk> = enemydef)
{
userDmg =(userAtk - enemydef)+ d4;
newEnemyHealth = enemyHealth - userDmg;
cmdHealth.Connection = conn;
cmdHealth.CommandText =" UPDATE characterStats将health = @newhealth FROM characterStats设置为cs WHERE cs.name = @name;" ;;
cmdHealth.Parameters.Add(new SqlParameter(" @ newhealth",SqlDbType.Int))。Value = newEnemyHealth;
cmdHealth.Parameters.AddWithValue(" @ name",enemy);
cmdHealth.ExecuteNonQuery();
}
Hello wrongholt,
似乎已经添加了 相同的参数多次。如果是这样,你需要在
之前添加cmdHealth。 command.Parameters.Clear方法 Parameters.Add nethod。结果应如下所示。if(userAtk> = enemydef)
{
userDmg =( userAtk - enemydef)+ d4;
newEnemyHealth = enemyHealth - userDmg;
cmdHealth.Connection = conn;
cmdHealth.command.Parameters.Clear();
cmdHealth.CommandText =" UPDATE characterStats将health = @newhealth FROM characterStats设置为cs WHERE cs.name = @name;" ;;
cmdHealth.Parameters.Add(new SqlParameter(" @ newhealth",SqlDbType.Int))。Value = newEnemyHealth;
cmdHealth.Parameters.AddWithValue(" @ name",enemy);
cmdHealth.ExecuteNonQuery();
}
祝你好运,
Neil Hu
This is in a function I call when the user attacks. I am trying to update the enemies health and it states @newhealth has already been declared. Should I use something else besides a UPDATE here? I also tried using AddWithValue("@newhealth",newEnemyHealth)
and stated the same thing.
if (userAtk >= enemydef) { userDmg = (userAtk - enemydef) + d4; newEnemyHealth = enemyHealth - userDmg; cmdHealth.Connection = conn; cmdHealth.CommandText = "UPDATE characterStats Set health = @newhealth FROM characterStats as cs WHERE cs.name = @name;"; cmdHealth.Parameters.Add(new SqlParameter("@newhealth", SqlDbType.Int)).Value = newEnemyHealth; cmdHealth.Parameters.AddWithValue("@name", enemy); cmdHealth.ExecuteNonQuery(); }
Hello wrongholt,
It seems that you have added the same parameters multiple times. If so you need add cmdHealth.command.Parameters.Clear method before Parameters.Add nethod. The result should be like below.
if (userAtk >= enemydef) { userDmg = (userAtk - enemydef) + d4; newEnemyHealth = enemyHealth - userDmg; cmdHealth.Connection = conn; cmdHealth.command.Parameters.Clear();
cmdHealth.CommandText = "UPDATE characterStats Set health = @newhealth FROM characterStats as cs WHERE cs.name = @name;"; cmdHealth.Parameters.Add(new SqlParameter("@newhealth", SqlDbType.Int)).Value = newEnemyHealth; cmdHealth.Parameters.AddWithValue("@name", enemy); cmdHealth.ExecuteNonQuery(); }
Best regards,
Neil Hu
这篇关于变量名'@ newhealth'已经被声明。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!