SQL,数据表。为什么值在列中的每一行都有变化?

时间:2022-09-17 10:54:46

So i writed something like editor but I have problem with value changing in every row in one column. I want to change just one specific cell not all. Also I used breakpoints and I realize that it happens when SqlCommand is defined and Executed but I don't know why it happens? I want e.g have two values the same but also want to it just change value in row which selected in comboxo not in all rows...

所以我写了一些类似编辑器的东西,但我在一列中的每一行都有值更改的问题。我想改变一个特定的细胞而不是全部。我也使用了断点,我意识到当SqlCommand被定义并执行时会发生这种情况,但我不知道它为什么会发生?例如,我想要两个相同的值,但也希望它只更改在comboxo中选择的行中的值而不是在所有行中...

private void button1_Click(object sender, EventArgs e)
    {
        UpdateBase();
        this.Close();
    }

    public string currentvalue = "";

    public void UpdateBase()
    {
        SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\JM\Documents\Kviz.mdf;Integrated Security=True;Connect Timeout=30");
        if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 0)
        {
            setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 0);

        }
        else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 1)
        {
            setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 1);
            setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 1);
            setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 1);
        }
        else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 2)
        {
            setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 2);
            setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 2);
            setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 2);
        }
        else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 3)
        {
            setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 3);
            setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 3);
            setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 3);
        }
        else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 4)
        {
            setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 4);
            setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 4);
            setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 4);
        }
        else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 5)
        {
            setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 5);
            setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 5);
            setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 5);
        }
        else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 6)
        {
            setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 6);
            setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 6);
            setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 6);
        }
        else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 7)
        {
            setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 7);
            setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 7);
            setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 7);
        }
        else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 8)
        {
            setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 8);
            setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 8);
            setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 8);
        }
        else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 9)
        {
            setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 9);
            setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 9);
            setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 9);
        }

    }




SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\JM\Documents\Kviz.mdf;Integrated Security=True;Connect Timeout=30");
public void setRadioButtonText(RadioButton RadioButton,string RadioButtonColumn, int row)
{

    ////Determine which radiobutton in datatable's row is selected
          //!//
    if (RadioButton.Checked == true)
    {
           //!//   
        RadioButton.Text = textBox1.Text;
        ////GET CURRENT VALUE
        SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM Pitanja", con);
        DataTable dt = new DataTable();
        ad.Fill(dt);
                              //!//       //!//
        currentvalue = dt.Rows[row][RadioButtonColumn].ToString();
        ////GET CURRENT VALUE
                                                                      //!//                                         //!//
        SqlCommand comnd = new SqlCommand("UPDATE Pitanja SET " + RadioButtonColumn + " = @textboxtext WHERE " + RadioButtonColumn + " LIKE '" + currentvalue + "'", con);
        comnd.Parameters.AddWithValue("@textboxtext", textBox1.Text);

        try
        {
            con.Open();
            comnd.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            MessageBox.Show(ex.ToString());
        }
        finally
        {
            con.Close();
        }
    }
}

Picture of the problem

问题的图片

2 个解决方案

#1


2  

Your problem lies in this part of your code

您的问题在于代码的这一部分

currentvalue = dt.Rows[row][RadioButtonColumn].ToString();
    ////GET CURRENT VALUE
                                                                  //!//                                         //!//
SqlCommand comnd = new SqlCommand("UPDATE Pitanja SET " + RadioButtonColumn + " = @textboxtext WHERE " + RadioButtonColumn + " LIKE '" + currentvalue + "'", con);
    comnd.Parameters.AddWithValue("@textboxtext", textBox1.Text);

You retrieve the value of the radiobuttoncolumn and update all 'Pitanja' where the value of the radiobuttoncolumn is equal to this value. Since all of your rows in the database are equal to this value. They will all be updated.

您检索radiobuttoncolumn的值并更新所有'Pitanja',其中radiobuttoncolumn的值等于此值。由于数据库中的所有行都等于此值。他们都将更新。

You should choose a unique value to filter your update, like your ID column. Then only the row with that unique Id will get updated. It will probably look a bit like this, however I didn't test this.

您应该选择一个唯一值来过滤更新,例如您的ID列。然后,只有具有该唯一ID的行才会更新。它可能看起来有点像这样,但我没有测试这个。

currentvalue = dt.Rows[row]["Id"].ToString();
    ////GET CURRENT VALUE
                                                                  //!//                                         //!//
SqlCommand comnd = new SqlCommand("UPDATE Pitanja SET " + RadioButtonColumn + " = @textboxtext WHERE Id = " + currentvalue, con);
    comnd.Parameters.AddWithValue("@textboxtext", textBox1.Text);

#2


0  

I tried code which send HansVG but didn't notice what HansVG sayed "Since all of your rows in the database are equal to this value. They will all be updated.", so I tried with Id until it worked...

我尝试了发送HansVG的代码,但没有注意到HansVG所说的“因为数据库中的所有行都等于这个值。它们都将被更新。”所以我尝试使用Id直到它工作...

Here is how it look like:

这是它的样子:

private void button1_Click(object sender, EventArgs e)
{
    UpdateujBazu();
    this.Close();
}

public string currentvalue = "";

public void UpdateujBazu()
{
    SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\JM\Documents\Kviz.mdf;Integrated Security=True;Connect Timeout=30");
    if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 0)
    {
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 0,1);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 0,1);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 0,1);
    }
    else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 1)
    {
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 1,2);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 1,2);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 1,2);
    }
    else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 2)
    {
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 2,3);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 2,3);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 2,3);
    }
    else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 3)
    {
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 3,4);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 3,4);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 3,4);
    }
    else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 4)
    {
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 4,5);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 4,5);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 4,5);
    }
    else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 5)
    {
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 5,6);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 5,6);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 5,6);
    }
    else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 6)
    {
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 6,7);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 6,7);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 6,7);
    }
    else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 7)
    {
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 7,8);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 7,8);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 7,8);
    }
    else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 8)
    {
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 8,9);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 8,9);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 8,9);
    }
    else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 9)
    {
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 9,10);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 9,10);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 9,10);
    }

}




SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\JM\Documents\Kviz.mdf;Integrated Security=True;Connect Timeout=30");
public void setRadioButtonText(RadioButton RadioButton,string RadioButtonColumn, int row,int ID)
{

    ////Determine which radiobutton in datatable's row is selected
          //!//
    if (RadioButton.Checked == true)
    {
           //!//   
        RadioButton.Text = textBox1.Text;
        ////GET CURRENT VALUE
        SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM Pitanja", con);
        DataTable dt = new DataTable();
        ad.Fill(dt);
                              //!//       //!//
        currentvalue = dt.Rows[row][RadioButtonColumn].ToString();
        ////GET CURRENT VALUE
                                                                      //!//                                         //!//
        SqlCommand comnd = new SqlCommand("UPDATE Pitanja SET " + RadioButtonColumn + " = '" + textBox1.Text + "'" + " WHERE " + RadioButtonColumn + " LIKE '" + currentvalue + "'" + " AND Id = '"  + ID + "'", con);

        try
        {
            con.Open();
            comnd.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            MessageBox.Show(ex.ToString());
        }
        finally
        {
            con.Close();
        }
    }
}

Picture

#1


2  

Your problem lies in this part of your code

您的问题在于代码的这一部分

currentvalue = dt.Rows[row][RadioButtonColumn].ToString();
    ////GET CURRENT VALUE
                                                                  //!//                                         //!//
SqlCommand comnd = new SqlCommand("UPDATE Pitanja SET " + RadioButtonColumn + " = @textboxtext WHERE " + RadioButtonColumn + " LIKE '" + currentvalue + "'", con);
    comnd.Parameters.AddWithValue("@textboxtext", textBox1.Text);

You retrieve the value of the radiobuttoncolumn and update all 'Pitanja' where the value of the radiobuttoncolumn is equal to this value. Since all of your rows in the database are equal to this value. They will all be updated.

您检索radiobuttoncolumn的值并更新所有'Pitanja',其中radiobuttoncolumn的值等于此值。由于数据库中的所有行都等于此值。他们都将更新。

You should choose a unique value to filter your update, like your ID column. Then only the row with that unique Id will get updated. It will probably look a bit like this, however I didn't test this.

您应该选择一个唯一值来过滤更新,例如您的ID列。然后,只有具有该唯一ID的行才会更新。它可能看起来有点像这样,但我没有测试这个。

currentvalue = dt.Rows[row]["Id"].ToString();
    ////GET CURRENT VALUE
                                                                  //!//                                         //!//
SqlCommand comnd = new SqlCommand("UPDATE Pitanja SET " + RadioButtonColumn + " = @textboxtext WHERE Id = " + currentvalue, con);
    comnd.Parameters.AddWithValue("@textboxtext", textBox1.Text);

#2


0  

I tried code which send HansVG but didn't notice what HansVG sayed "Since all of your rows in the database are equal to this value. They will all be updated.", so I tried with Id until it worked...

我尝试了发送HansVG的代码,但没有注意到HansVG所说的“因为数据库中的所有行都等于这个值。它们都将被更新。”所以我尝试使用Id直到它工作...

Here is how it look like:

这是它的样子:

private void button1_Click(object sender, EventArgs e)
{
    UpdateujBazu();
    this.Close();
}

public string currentvalue = "";

public void UpdateujBazu()
{
    SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\JM\Documents\Kviz.mdf;Integrated Security=True;Connect Timeout=30");
    if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 0)
    {
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 0,1);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 0,1);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 0,1);
    }
    else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 1)
    {
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 1,2);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 1,2);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 1,2);
    }
    else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 2)
    {
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 2,3);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 2,3);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 2,3);
    }
    else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 3)
    {
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 3,4);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 3,4);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 3,4);
    }
    else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 4)
    {
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 4,5);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 4,5);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 4,5);
    }
    else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 5)
    {
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 5,6);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 5,6);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 5,6);
    }
    else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 6)
    {
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 6,7);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 6,7);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 6,7);
    }
    else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 7)
    {
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 7,8);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 7,8);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 7,8);
    }
    else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 8)
    {
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 8,9);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 8,9);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 8,9);
    }
    else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 9)
    {
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 9,10);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 9,10);
        setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 9,10);
    }

}




SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\JM\Documents\Kviz.mdf;Integrated Security=True;Connect Timeout=30");
public void setRadioButtonText(RadioButton RadioButton,string RadioButtonColumn, int row,int ID)
{

    ////Determine which radiobutton in datatable's row is selected
          //!//
    if (RadioButton.Checked == true)
    {
           //!//   
        RadioButton.Text = textBox1.Text;
        ////GET CURRENT VALUE
        SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM Pitanja", con);
        DataTable dt = new DataTable();
        ad.Fill(dt);
                              //!//       //!//
        currentvalue = dt.Rows[row][RadioButtonColumn].ToString();
        ////GET CURRENT VALUE
                                                                      //!//                                         //!//
        SqlCommand comnd = new SqlCommand("UPDATE Pitanja SET " + RadioButtonColumn + " = '" + textBox1.Text + "'" + " WHERE " + RadioButtonColumn + " LIKE '" + currentvalue + "'" + " AND Id = '"  + ID + "'", con);

        try
        {
            con.Open();
            comnd.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            MessageBox.Show(ex.ToString());
        }
        finally
        {
            con.Close();
        }
    }
}

Picture