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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
| 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 System.Data.Common;
using System.Data.ProviderBase;
namespace test_connexion_BDD
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void connex_Click(object sender, EventArgs e)
{
//_____________________________________________________Button Connexion___________________________
// Connexion à la base de données
SqlConnection connexion = new SqlConnection();
connexion.ConnectionString = @"Data Source=localhost\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI";
try
{
connexion.Open(); // Ouverture de la connexion
}
catch (Exception ex)
{
// Affiche des erreurs
Console.WriteLine(ex.Message);
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void val_Click(object sender, EventArgs e)
{
//_____________________________________________________Button Valider____________________________
if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "")
{
MessageBox.Show("Vous devez remplir Tous les champs pour valider le formulaire", "Saisie incomplète", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
try
{
using (SqlConnection connexion = new SqlConnection(connectionString))
{
connexion.Open();//--Ouverture de connexion
//-------------------------------------Requête SQL-----------------------------
SqlCommand sqlCommand = new SqlCommand("INSERT INTO user (ID,Nom , Prenom , soldes) values (@ID, @Nom , @Prenom , @Sodes) ", connexion);
//-----------------------------Création de variable de Textbox------------------
//Tesxt Box Id
sqlCommand.Parameters.Add(
"@ID",SqlDbType.VarChar);
sqlCommand.Parameters[
"@ID"].Value= textBox1.Text;
//textbox -> variable @Nom
sqlCommand.Parameters.Add(
"@Nom", SqlDbType.VarChar);
sqlCommand.Parameters[
"@Nom"].Value = textBox2.Text;
//textbox -> variable @Prenom
sqlCommand.Parameters.Add(
"@Prenom", SqlDbType.VarChar);
sqlCommand.Parameters[
"@Prenom"].Value = textBox3.Text;
//textbox -> variable @Unites
sqlCommand.Parameters.Add(
"@Soldes", SqlDbType.VarChar);
sqlCommand.Parameters[
"@Soldes"].Value = textBox4.Text;
//-- Execution de la commande ----
sqlCommand.ExecuteNonQuery();
}
}
catch (Exception exep) { System.Windows.Forms.MessageBox.Show(exep.Message); }
}
}
} |
Partager