Updating values in local dataBase error: CommandText property has not been initialized

Updating values in local dataBase error: CommandText property has not been initialized

我创建了某种应用程序来保存员工及其付款的数据库。到目前为止效果很好。但是现在我正在尝试实现一个 "update" 功能,如果有一些数据会针对特定用户发生变化。

所以我为更新编写了以下代码,但出现此错误:

CommandText property has not been initialized at line 105: "cmd.ExecuteNonQuery();"

谢谢!

var connString = @"Data Source=C:\Users\Andrei\Documents\Visual Studio 2010\Projects\Stellwag\Stellwag\Angajati.sdf";

using (var conn = new SqlCeConnection(connString))
{
    try
    {
        conn.Open();

        SqlCeCommand cmd = new SqlCeCommand();
        //conecteaza cmd la conn
        cmd.Connection = conn;

        //adauga parametru pt campul poza cu value image
        SqlCeParameter picture = new SqlCeParameter("@Poza", SqlDbType.Image);

        MemoryStream ms = new MemoryStream();
        pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
        byte[] a = ms.GetBuffer();
        ms.Close();

        cmd.Parameters.Clear();
        cmd.Parameters.AddWithValue("@Poza", a);

        var query = "UPDATE info SET Nume='" + textBox5.Text + "' AND Prenume='" + textBox4.Text + "' AND Data='" + dateTimePicker1.Value.ToShortDateString() + "' AND Proiect='" + textBox1.Text + "' AND Schimburi='" + label10.Text + "' AND Poza=@Poza AND Acord='" + textBox2.Text + "' AND Baza='" + textBox3.Text + "'  WHERE Nume='" + label8.Text + "' AND Prenume='" + label5.Text + "'";

        cmd.ExecuteNonQuery();

        MessageBox.Show("Salvat cu succes!");
        this.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

您必须设置cmd.CommandText

                //Codes
                cmd.CommandText = query;
                cmd.ExecuteNonQuery();
                MessageBox.Show("Salvat cu succes!");
                this.Close();

在您的执行之上添加 cmd.CommandText = query;