首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >SQL,DataTable.为什么值在列的每一行中都会发生变化?

SQL,DataTable.为什么值在列的每一行中都会发生变化?
EN

Stack Overflow用户
提问于 2016-09-30 23:03:42
回答 2查看 59关注 0票数 0

因此,我写了一些类似编辑器的内容,但在一列的每一行中,值的变化都有问题。我只想换一个特定的细胞,而不是全部。另外,我使用了断点,我意识到当定义和执行SqlCommand时会发生这种情况,但我不知道为什么会发生这种情况?我希望有两个值相同,但也希望它只是改变在行中选择的值,而不是在所有行.

代码语言:javascript
运行
复制
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();
        }
    }
}

问题的图景

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-09-30 23:27:19

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

代码语言:javascript
运行
复制
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);

检索放射按钮柱的值,并更新所有“Pitanja”,其中放射按钮列的值等于此值。因为数据库中的所有行都等于此值。它们都会被更新。

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

代码语言:javascript
运行
复制
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
EN

Stack Overflow用户

发布于 2016-10-01 08:29:56

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

以下是它的样子:

代码语言:javascript
运行
复制
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();
        }
    }
}

图片

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39801263

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档