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

时间:2015-06-23 19:47:25

标签: c# mysql parameters

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

    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'不存在,但那是参数...... 现在,我没有收到任何错误,但它不会更新我的数据库。 请帮忙

P.S。请忽略sql注入,在此之前,在我发现参数之前,我并不知道更好。

3 个答案:

答案 0 :(得分:0)

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

function isMessageFromIFrame(event,iframe){
    var
        origin  = event.origin,
        src     = iframe.src;

    if ((''+origin !== 'null') && (origin !== src.substr(0,origin.length))) {
        throw new Error(
            'Unexpect message received from: ' + origin +
            ' for ' + iframe.id + '. Message was: ' + event.data  
        );
    }

    return true;
}

答案 1 :(得分:0)

我认为你做错了就是你不能像那样初始化你的MySqlCommand。它一定是这样的..

<configuration>
    <suiteXmlFiles>
        <suiteXmlFile>${testSuite}</suiteXmlFile>
    </suiteXmlFiles>
</configuration>

答案 2 :(得分:-1)

当我在c#中创建新数据时,我就这样做..

//值

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

//这是mysql的代码

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

// conn是你的mysqlconnection

MySqlCommand cmd = new MySqlCommand(query,conn);

//然后执行它

cmd.ExecuteNonQuery();