连接到数据库的复选框

时间:2019-03-19 12:38:36

标签: c# sql wpf checkbox

我遇到了有关从WPF应用程序到SQL数据库的复选框的问题。

我到达了可以将特定项目的值更新到数据库中的位置,但是我只能将值更新或从0到1或从1到0进行更新,我正在寻找IF语句类型问题,我想我已经解决了,但是代码无法正常工作,并给了我错误

  

“” =“

附近有错误

请帮助。

private void Row_DoubleClick(object sender, MouseButtonEventArgs e)
{
    sqliteCon.Open();

    if (sqliteCon.State == System.Data.ConnectionState.Open)
    {
    var currentRowIndex = dataGrid1.Items.IndexOf(dataGrid1.CurrentItem);//PER IDENTIFICARE LA LINEA CORRENTE

    //PROVA5
    string q = @"UPDATE tabList
         SET selection = (CASE Prova
                            WHEN tabList.selection.Value = ' 0 '
                            THEN tabList.selection.Value = ' 1'
                            ELSE tabList.selection.Value = ' 0 '
                        END)
     WHERE idL = @CURRENT";


        SqlCommand cmd = new SqlCommand(q, sqliteCon);
    cmd.Parameters.AddWithValue("@CURRENT", currentRowIndex = currentRowIndex + 1);
    cmd.ExecuteNonQuery();
        MessageBox.Show("Dato Modificato");
    }

    sqliteCon.Close();
}

2 个答案:

答案 0 :(得分:2)

SQL语句看不到复选框的值。就像ID参数的参数一样,您需要将其从复选框中拉出并将THAT应用于sql查询

var flagVal = WPFControl.selection.value == 1 ? 0 : 1;

//PROVA5
var q = @"UPDATE tabList
             SET selection = @NEWFLAG
             WHERE idL = @CURRENT";
cmd.Parameters.AddWithValue("@NEWFLAG", flagVal);

... rest of your code 

答案 1 :(得分:0)

 private void Row_DoubleClick(object sender, MouseButtonEventArgs e)
    {

        sqliteCon.Open();

        if (sqliteCon.State == System.Data.ConnectionState.Open)
        {

            var currentRowIndex = dataGrid1.Items.IndexOf(dataGrid1.CurrentItem);//PER IDENTIFICARE LA LINEA CORRENTE


            string q = @"UPDATE tabList
                         SET selection = (CASE 
                                            WHEN (SELECT selection FROM tabList where idL = @CURRENT) = 0
                                            THEN 1
                                            ELSE 0
                                          END)
                         WHERE idL = @CURRENT";


            SqlCommand cmd = new SqlCommand(q, sqliteCon);
            currentRowIndex = currentRowIndex + 1;
            cmd.Parameters.AddWithValue("@CURRENT", currentRowIndex);
            cmd.ExecuteNonQuery();
            MessageBox.Show("Dato Modificato");

        }
        sqliteCon.Close();
    }