0% found this document useful (0 votes)
39 views3 pages

C# Ado - Net Mode Connecté

This document contains C# code for connecting to a SQL database and performing CRUD (create, read, update, delete) operations. It defines methods for displaying data in a datagrid, populating dropdowns from the database, getting an ID value, adding/modifying/deleting records, searching, navigating records, and validating record existence. The methods open a SQL connection, execute commands, and close the connection to retrieve and manipulate data from various database tables.

Uploaded by

Nabil Sabii
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views3 pages

C# Ado - Net Mode Connecté

This document contains C# code for connecting to a SQL database and performing CRUD (create, read, update, delete) operations. It defines methods for displaying data in a datagrid, populating dropdowns from the database, getting an ID value, adding/modifying/deleting records, searching, navigating records, and validating record existence. The methods open a SQL connection, execute commands, and close the connection to retrieve and manipulate data from various database tables.

Uploaded by

Nabil Sabii
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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