c#使用参数将数据插入MySQL数据库

本文关键字:插入 MySQL 数据库 数据 参数 | 更新日期: 2023-09-27 17:59:10

这可能是一个简单的解决方案,但我有一个截止日期,我不知道这里的确切问题。所以交易是这样的,我正试图使用以下代码更新我的表:

    private void btn_opslaan_Click(object sender, EventArgs e)
    {
        string opleidingsid = "Select OpleidingsID From opleidingen Where Opleidingsnaam = '" + cb_opleiding.Text + "'";
        MySqlCommand cmdid = new MySqlCommand(opleidingsid, dbconnect.connection);
        dbconnect.OpenConnection();
        MySqlDataReader reader = cmdid.ExecuteReader();
        reader.Read();
            int oplid = (int)reader.GetValue(0);
        cmdid.Dispose();
        reader.Close();
        sql = "UPDATE leerlingen SET Naam = '_naam', Adres = '_adres', Woonplaats = '_woonplaats', Postcode = '_postcode', Email = '_email', Telefoonnummer = '_telefoonnummer', Klas = '_klas', Ovnummer = '_ovnummer', OpleidingsID = '_opleidingsid', Startdatum = '_startdatum', Einddatum = '_einddatum' WHERE LeerlingID = '_leerlingid'";
      //  sql = "UPDATE leerlingen set Naam  = '" + txt_naam.Text + "', Adres = '" + txt_adres.Text + "', Woonplaats = '" + txt_woonplaats.Text + "', Postcode = '" + txt_postcode.Text + "', Email = '" + txt_email.Text + "', Telefoonnummer = '" + txt_telefoonnumer.Text + "', Klas = '" + txt_klas.Text + "', Ovnummer = '" + txt_ovnummer.Text + "', OpleidingsID = '" + oplID + "', Startdatum = '"+mc_startdatum.SelectionStart.Date.ToString()+"', Einddatum = '"+ mc_einddatum.SelectionStart.Date.ToString() +"' WHERE LeerlingID = '" + Int32.Parse(lbl_leerlingid.Text) + "'";
        MySqlCommand cmd = new MySqlCommand(sql, dbconnect.connection);
        cmd.Parameters.AddWithValue("_naam", txt_naam.Text);
        cmd.Parameters.AddWithValue("_adres", txt_adres.Text);
        cmd.Parameters.AddWithValue("_woonplaats", txt_woonplaats.Text);
        cmd.Parameters.AddWithValue("_postcode", txt_postcode.Text);
        cmd.Parameters.AddWithValue("_email", txt_email.Text);
        cmd.Parameters.AddWithValue("_telefoonnummer", txt_telefoonnumer.Text);
        cmd.Parameters.AddWithValue("_klas", txt_klas.Text);
        cmd.Parameters.AddWithValue("_ovnummer", txt_ovnummer.Text);
        cmd.Parameters.AddWithValue("_opleidingsid", oplid);
        cmd.Parameters.AddWithValue("_startdatum", mc_startdatum.SelectionStart.Date.ToString());
        cmd.Parameters.AddWithValue("_einddatum", mc_einddatum.SelectionStart.Date.ToString());
        cmd.Parameters.AddWithValue("_leerlingid", int.Parse(lbl_leerlingid.Text));
        try
        {
            cmd.ExecuteNonQuery();
            MessageBox.Show("opslaan gelukt");
        }
        catch (Exception error)
        {
            MessageBox.Show(error.ToString());
            throw;
        }
        dbconnect.CloseConnection();
        this.Close();
    }

我已经尝试过不使用单引号,这会给我一个错误,即colomn'_leerlingid'不存在,但这就是参数。。。现在,我没有收到任何错误,但它不会更新我的数据库。请帮忙

附言:请忽略sql注入,在此之前,在我发现参数之前,我并不知道得更好。

c#使用参数将数据插入MySQL数据库

尝试用@符号替换参数并删除单引号,如下所示:

SQL = "UPDATE leerlingen SET Naam = @naam, Adres = @adres";
cmd.Parameters.AddWithValue("@naam", txt_naam.Text);
cmd.Parameters.AddWithValue("@adres", txt_adres.Text);

我认为您做错了什么,不能这样初始化MySqlCommand。一定是这样的。。

MySqlCommand cmd;
cmd = dbconnect.createCommand();
cmd.CommandText = "UPDATE tableName SET firstname=@firstname, lastname=@lastname where id=@id";
cmd.Parameters.AddWithValue("@id", idTxt.Text);
cmd.Parameters.AddWithValue("@firstname", fName.Text);
cmd.Parameters.AddWithValue("@lastname", lName.Text);
cmd.ExecuteNonQuery();

当我在c#中创建新数据时,我会把它做成这样。。

//值

String a = "COL1ROW1", b = "COL1ROW2";

//这是mysql 的代码

String query = "Insert Into tableName(Column1, Column2)values('" + a + "','" + b + "')";

//conn是您的mysqlconnection

MySqlCommand cmd=新MySqlCommand(query,conn);

//然后执行

cmd.ExecuteNonQuery();