简体   繁体   中英

Why does my ExecuteNonQuery() return 0 in C#

我正在尝试使用新的TeamID更新玩家记录,但未更新,请提示

ExecuteNonQuery returns the number of rows affected. There is no Person_ID with value 0 in your database.

It looks like you might want to move a large portion of your code inside your while loop, like most of this

SqlCommand Updatecmd = new SqlCommand("UPDATE Persons SET TeamID=@Team_ID WHERE PersonID=@Person_ID", UpdateCon);
Updatecmd.Parameters.Add("@Team_ID", SqlDbType.VarChar);
Updatecmd.Parameters.Add("@Person_ID", SqlDbType.VarChar);
Updatecmd.Parameters["@Team_ID"].Value = PlayerTeam_ID;
Updatecmd.Parameters["@Person_ID"].Value = Player_ID;

Should be put after this

 PlayersCount++;
 Division_ID=reader.GetInt32(1);
 Player_ID = reader.GetInt32(0); 
 PlayerTeam_ID = PlayersCount % 4 + 4 * Division_ID;

These two lines:

Updatecmd.Parameters["@Team_ID"].Value = PlayerTeam_ID;
Updatecmd.Parameters["@Person_ID"].Value = Player_ID;

Set the value initially to the variables PlayerTeam_ID and Player_ID . In your read loop, you're updating the variables PlayerTeam_ID and Player_ID , but you're not updating the values in the parameters. You should update the value of the parameters, like this:

while (reader.Read())
{
    PlayersCount++;
    Division_ID=reader.GetInt32(1);
    Player_ID = reader.GetInt32(0); 
    PlayerTeam_ID = PlayersCount % 4 + 4 * Division_ID;

    Updatecmd.Parameters["@Team_ID"].Value = PlayerTeam_ID;
    Updatecmd.Parameters["@Person_ID"].Value = Player_ID;

    UpdateCon.Open();
    int intQuery = Updatecmd.ExecuteNonQuery();

    ltlOutput.Text = intQuery.ToString();

    UpdateCon.Close();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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