Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

// Déclaration

using System;
using System.Data.SqlClient;
using System.Windows.Forms;

SqlConnection con = new SqlConnection(@"Data Source = .\SQLEXPRESS ; Initial


Catalog= ...(Nom de database) ; Integrated Security = True");
SqlCommand cmd;
SqlDataReader dr;
BindingSource bs = new BindingSource();

// Afficher

private void Afficher()


{
con.Open();
cmd = new SqlCommand("select * from ...(Nom de table)", con);
dr = cmd.ExecuteReader();
bs.DataSource = dr;
dataGridView1.DataSource = bs;
con.Close();
}

// Remplisage de comboBox

private void RemplirComboBox()


{
cmd = new SqlCommand("select ...()) from ...", con);
con.Open();
dr = cmd.ExecuteReader();
while (dr.Read())
{
comboBox1.Items.Add(dr[0].ToString());
}
dr.Close();
con.Close();
}

// Get ID

private int getIdAcademie(String nom)


{
cmd = new SqlCommand("select ...() from ...() where ...() = '" + nom + "'",
con);
con.Open();
int i = (int)cmd.ExecuteScalar();
con.Close();
return i;
}

// Ajouter

con.Open();
cmd = new SqlCommand("select * from ...(Nom de table)", con);
dr = cmd.ExecuteReader();
bs.DataSource = dr;
dataGridView1.DataSource = bs;
dr.Close();
con.Close();
MessageBox.Show("Bien Ajouter");

// Modifier

con.Open();
SqlCommand cmd = new SqlCommand("update ...(Nom de table) set ...(exemple nom) = '"
+ textBox1.Text + "' , ...(exemple prenom) = '" + textBox2.Text + "' where ...(id de
table) = '" + comboBox1.Text + "'", con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Bien Modifier");

// Supprimer

con.Open();
cmd = new SqlCommand("delete from ...(nom de table) where ...(id de table) ='" +
textBox1.Text + "'", con);
cmd.ExecuteNonQuery();
con.Close();

// Rechercher

con.Open();
cmd = new SqlCommand("select * from (nom de table) where ...(id de table) ='" +
textBox1.Text + "'", con);
dr = cmd.ExecuteReader();
bs.DataSource = dr;
dataGridView1.DataSource = bs;
dr.Close();
con.Close();

// Vider

textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
...

// Form Load

Afficher();
navigation();
RemplirComboBox();

// Exist

private Boolean Exist()


{
cmd = new SqlCommand("select count(...) from Acteur where ...='" +
int.Parse(textBox1.Text) + "', con);
con.Open();
int count = (int)cmd.ExecuteScalar();
if (count == 1)
{
return true;
}
else
{
return false;
}
con.Close();
}

// Navigation

private void navigation()


{
cmd = new SqlCommand("select ...() as '...()' ,... as '...' from ...", con);
con.Open();
dr = cmd.ExecuteReader();
bs.DataSource = dr;
textBox1.DataBindings.Add(new Binding("text", bs, "...()"));
textBox2.DataBindings.Add(new Binding("text", bs, "...()"));
indexLabel.Text = (bs.Position + 1).ToString();
totalLabel.Text = bs.Count.ToString();
dataGridView1.DataSource = bs;
dr.Close();
con.Close();
}

// button Premier

bs.MoveFirst();
indexLabel.Text = (bs.Position + 1).ToString();
totalLabel.Text = bs.Count.ToString();

// button Précédent

bs.MovePrevious();
indexLabel.Text = (bs.Position + 1).ToString();
totalLabel.Text = bs.Count.ToString();

// button Suivant

bs.MoveNext();
indexLabel.Text = (bs.Position + 1).ToString();
totalLabel.Text = bs.Count.ToString();

// button Dernier

bs.MoveLast();
indexLabel.Text = (bs.Position + 1).ToString();
totalLabel.Text = bs.Count.ToString();

You might also like