无法更新mysql数据库c#

gpnt7bae  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(325)

我正在尝试从不同类型的控件更新数据库,这些控件是在gridview中选择元素时填充的,但是我的代码不起作用,我不知道还能做什么。事先谢谢你的帮助。下面是我的代码:
更新数据库的主要方法:

public static void Editez_Nota(int idintrare, int idspecializare, int idnivelstudii, int idanstudiu, int idstudent, int idmaterie, int idprof, double iddnota, DateTime dataEvaluarii)
    {
        conn.Open();
        MySqlCommand editezNota = new MySqlCommand("UPDATE catalog SET iddspec=@iddspec, iddnivel=@iddnivel, " +
            "iddan=@iddan, iddstudent=@iddstudent, iddmaterie=@iddmaterie, iddprof=@iddprof, " +
            "iddnota=@iddnota, idddata=CAST(@idddata as datetime) WHERE idintrare=@idintrare", conn);
        try
        {

            editezNota.Parameters.AddWithValue("@idintrare", idintrare);**//this was missing**
            editezNota.Parameters.AddWithValue("@iddspec", idspecializare);
            editezNota.Parameters.AddWithValue("@iddnivel", idnivelstudii);
            editezNota.Parameters.AddWithValue("@iddan", idanstudiu);
            editezNota.Parameters.AddWithValue("@iddstudent", idstudent);
            editezNota.Parameters.AddWithValue("@iddmaterie", idmaterie);
            editezNota.Parameters.AddWithValue("@iddprof", idprof);
            editezNota.Parameters.AddWithValue("@iddnota", iddnota);
            editezNota.Parameters.AddWithValue("@idddata", dataEvaluarii);
            editezNota.ExecuteNonQuery();
            conn.Close();
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

保存按钮:

private void button2_Click(object sender, EventArgs e)
    {
        try
        {
            int rowindex = dataGridViewNote.CurrentRow.Index;
            int id_intrare = Convert.ToInt32(dataGridViewNote.Rows[rowindex].Cells[0].Value.ToString());
            int idspecializare = Convert.ToInt32(comboBoxSpec.SelectedValue);
            int idnivelstudii = Convert.ToInt32(comboBoxNivel.SelectedValue);
            int idanstudiu = Convert.ToInt32(comboBoxAn.SelectedValue);
            int idstudent = Convert.ToInt32(comboBoxStudent.SelectedValue);
            int idmaterie = Convert.ToInt32(comboBoxMaterie.SelectedValue);
            int idprof = Convert.ToInt32(comboBoxProf.SelectedValue);
            double iddnota = Convert.ToDouble(textNota.Text);

            DB_Catalog.Editez_Nota(id_intrare, idspecializare, idnivelstudii, idanstudiu, idstudent, idmaterie, idprof, iddnota, dateTimeevaluare.Value.Date);
            dataGridViewNote.DataSource = DB_Catalog.Populez_Note();

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
vd8tlhqk

vd8tlhqk1#

我设法修好了。问题在于它无法识别表的主键(where子句),因为我没有声明在datagrid中存储所选项的主键的变量。我会编辑代码,以防别人犯同样愚蠢的错误谢谢大家的回答!

相关问题