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
|
public void Ajouter_Click(object sender, EventArgs e)
{
//on vérifi que tout les champs sont remplis
if ( this.loginTextBox.Text == "" || this.passTextBox.Text =="" || this.nomTextBox.Text == "" || this.prenomTextBox.Text == "")
{
MessageBox.Show("Veuillez remplir tout les champs");
}
else
{
//source : livre introduction c# 2008 p 228
string strConnexion = ConfigurationManager.ConnectionStrings["Pyramide.Properties.Settings.centreConnectionString"].ConnectionString;
string requete;
try
{
SqlConnection oConnection = new SqlConnection(strConnexion);
// requete insert
requete = "INSERT INTO [user] (login_user, pass_user, nom_user, prenom_user) VALUES (@login_user, @pass_user, @nom_user, @prenom_user)";
SqlCommand oCommand = new SqlCommand (requete, oConnection);
//Création et décalartion des paramètres
oCommand.Parameters.Add(new SqlParameter("@login_user", SqlDbType.NVarChar, 50));
oCommand.Parameters.Add(new SqlParameter("@pass_user", SqlDbType.NVarChar, 50));
oCommand.Parameters.Add(new SqlParameter("@nom_user", SqlDbType.NVarChar, 50));
oCommand.Parameters.Add(new SqlParameter("@prenom_user", SqlDbType.NVarChar, 50));
//Attribution des valeurs aux paramètres
oCommand.Parameters["@login_user"].Value = loginTextBox.Text ;
oCommand.Parameters["@pass_user"].Value = passTextBox.Text;
oCommand.Parameters["@nom_user"].Value = nomTextBox.Text;
oCommand.Parameters["@prenom_user"].Value = prenomTextBox.Text;
oCommand.Connection.Open();
oCommand.ExecuteNonQuery();
MessageBox.Show("Ajout effectué avec succés");
//raffraichir le datagrid
// On remplit le DataSet
this.userTableAdapter.Fill(this.centreDataSet.user);
// On remplit l'affichage de la grille
this.userBindingSource.DataSource = this.centreDataSet;
this.dataGridView1.DataSource = this.userBindingSource;
oCommand.Connection.Close();
}
catch (Exception x)
{
MessageBox.Show("L'erreur suivante a été rencontrée :" + x.Message);
} |
Partager