记录未在数据库中更新

时间:2013-10-19 08:37:19

标签: c# sql-server-ce

我无法更新数据库中的记录。

我的代码如下:

private void update_Click(object sender, EventArgs e)
{
        con.Open();

         try

         {
        string strQuery = "UPDATE test_name SET Test_Name = @name, Price = @price, Lab_Price = @lprice, normal_value_child = @child, normal_value_men = @men, normal_value_women = @women, notes = @note, unit = @un, SubGroup = @sub, MainGroup = @main" +
              "WHERE Test_Id = @tstID";
        using (System.Data.SqlServerCe.SqlCeCommand cmdSelect = new System.Data.SqlServerCe.SqlCeCommand(strQuery, con))
        {
            cmdSelect.Parameters.AddWithValue("@tstID", richTextBox2.Text);
            cmdSelect.Parameters.AddWithValue("@name", richTextBox1.Text);
            cmdSelect.Parameters.AddWithValue("@price", richTextBox7.Text);
            cmdSelect.Parameters.AddWithValue("@lprice", richTextBox8.Text);
            cmdSelect.Parameters.AddWithValue("@child", richTextBox5.Text);
            cmdSelect.Parameters.AddWithValue("@men", richTextBox4.Text);
            cmdSelect.Parameters.AddWithValue("@women", richTextBox3.Text);
            cmdSelect.Parameters.AddWithValue("@note", richTextBox9.Text);
            cmdSelect.Parameters.AddWithValue("@un", richTextBox6.Text);
            cmdSelect.Parameters.AddWithValue("@sub", comboBox2.Text);
            cmdSelect.Parameters.AddWithValue("@main", comboBox1.Text);
            cmdSelect.ExecuteNonQuery();
            updatedb();
            con.Close();
        }
    }
    catch (Exception ex)
    {

    }

    this.test_nameTableAdapter2.Fill(this.datalabDataSet14.test_name);

    MessageBox.Show("Updated Successfully");
}
}

我不明白这是什么问题。

1 个答案:

答案 0 :(得分:2)

在WHERE之前添加空格:

string strQuery = "UPDATE test_name SET Test_Name = @name, Price = @price, Lab_Price = @lprice, normal_value_child = @child, normal_value_men = @men, normal_value_women = @women, notes = @note, unit = @un, SubGroup = @sub, MainGroup = @main" +
          " WHERE Test_Id = @tstID";