更新语句更改错误记录

Update statement changes wrong records

问题

如何编辑任何列值的行,并在没有增量列的情况下更改右行?

代码

 public partial class EditUser : Form
{
    public DataGridViewRow dgvRow;
    public EditUser()
    {
        InitializeComponent();
    }

    private void EditUser_Load(object sender, EventArgs e)
    {
        lbl5.Text = dgvRow.Cells[0].Value.ToString();
        txtUserID.Text = dgvRow.Cells[0].Value.ToString();
        txtFName.Text = dgvRow.Cells[1].Value.ToString();
        txtLName.Text = dgvRow.Cells[2].Value.ToString();
        txtEmail.Text = dgvRow.Cells[3].Value.ToString();
        label1.Visible = false;
        txtUserID.Visible = false;
    }

    private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
        this.Visible = false;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        SqlConnection sc = new SqlConnection();
        SqlCommand com = new SqlCommand();
        sc.ConnectionString = ("");
        sc.Open();
        com.Connection = sc;
        com.CommandText = ("update JoshUserTable SET UserID = @UserID, FirstName=@FirstName,LastName=@LastName,Email=@Email where UserID = @UserID");
        com.Parameters.AddWithValue("@UserID", txtUserID.Text);
        com.Parameters.AddWithValue("@FirstName", txtFName.Text);
        com.Parameters.AddWithValue("@LastName", txtLName.Text);  
        com.Parameters.AddWithValue("@Email", txtEmail.Text);  
        com.ExecuteNonQuery();
        sc.Close();
    }

    private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
        label1.Visible = true;
        txtUserID.Visible = true;
        SqlConnection sc = new SqlConnection();
        SqlCommand con = new SqlCommand();
        sc.ConnectionString = ("");
        sc.Open();
        con.Connection = sc;
        con.CommandText = ("update JoshUserTable SET UserID = @UserID, FirstName=@FirstName,LastName=@LastName,Email=@Email where FirstName = @FirstName");
        con.Parameters.AddWithValue("@UserID", txtUserID.Text);
        con.Parameters.AddWithValue("@FirstName", txtFName.Text);
        con.Parameters.AddWithValue("@LastName", txtLName.Text);
        con.Parameters.AddWithValue("@Email", txtEmail.Text);
        con.ExecuteNonQuery();
        sc.Close();
    }
}

我认为您 运行 遇到了一个更大的问题。如果记录被另一个用户同时更改怎么办。您将 运行 遇到 并发问题 。这就是 CommandBuilder 的自动生成代码总是检查旧值的原因。

对于您当前的代码,我添加了对原始用户 ID 的检查。

private void button1_Click(object sender, EventArgs e)
{
    SqlConnection sc = new SqlConnection();
    SqlCommand com = new SqlCommand();
    sc.ConnectionString = ("My Connection String");
    sc.Open();
    com.Connection = sc;

    /// check for original value in whereClause
    com.CommandText = ("update JoshUserTable SET UserID = @NEW_UserID,
                        FirstName=@FirstName,LastName=@LastName,Email=@Email
                        where UserID = @ORIGINAL_UserID");


    com.Parameters.AddWithValue("@NEW_UserID", txtUserID.Text);
    com.Parameters.AddWithValue("@ORIGINAL_UserID", INSERT_ORIGINAL_USERID);


    com.Parameters.AddWithValue("@FirstName", txtFName.Text);
    com.Parameters.AddWithValue("@LastName", txtLName.Text);  
    com.Parameters.AddWithValue("@Email", txtEmail.Text);  
    com.ExecuteNonQuery();
    sc.Close();
}

并发问题还没解决....