1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
MySqlConnection maConnexion = new MySqlConnection("Server=localhost;Database=bdTest;Uid=root;Pwd=;");
string maRequete;
MySqlCommand myCommand;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
/* MySqlCommand maCommande = new MySqlCommand();
maCommande.Connection = maConnexion;
maConnexion.Open();
maCommande.CommandText = "INSERT INTO tbltest (fldnum, fldnom, fldmdp) VALUES ("+ textBoxNum.Text + ", '" + textBoxNom.Text + "', '"+ textBoxMdp.Text +"');";
maConnexion.Close(); */
//Requete mise a jour de la table client
maRequete = "INSERT INTO [tbltest] ( [fldNum],[fldNom],[fldMdp]) VALUES (@Num, @Nom, @Mdp)";
//Création de la commande SQL
myCommand = new MySqlCommand(maRequete, maConnexion);
//Création et décalartion des paramètres
myCommand.Parameters.Add(new MySqlParameter("@Num", MySqlDbType.Int32, 11));
myCommand.Parameters.Add(new MySqlParameter("@Nom", MySqlDbType.VarChar, 15));
myCommand.Parameters.Add(new MySqlParameter("@Mdp", MySqlDbType.Int32, 11));
//Attribution des valeurs aux paramètres
myCommand.Parameters["@Num"].Value = Convert.ToInt32(textBoxNum.Text);
myCommand.Parameters["@Nom"].Value = textBoxNom.Text;
myCommand.Parameters["@Mdp"].Value = Convert.ToInt32(textBoxMdp.Text);
try
{
//Execution de la requête
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
}
}
} |
Partager