Mar.23
TP9 : opérations de recherche
Objectif
Dans ce TP vous allez effectuer des opérations de recherche.
Questions
Partie serveur :
- Créer la base Gestion.
- Dans la base Gestion, crée la table client:
Partie client :
- Créer l’interface suivante :
- Écrire le code de boutons Chercher.
Correction
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; namespace TP8_Rechercher { public partial class Form1 : Form { SqlConnection conn = new SqlConnection("server = DESKTOP-EIALG0J\\SQLEXPRESS; database = gestion; integrated security = SSPI"); SqlDataReader dr; SqlCommand cmd; BindingSource source = new BindingSource(); DataTable dt = new DataTable(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { cmd = new SqlCommand("select ldClient as 'Id', nomClient as 'Nom', villeClient as 'Ville' from client", conn); conn.Open(); dr = cmd.ExecuteReader(); dt.Load(dr); source.DataSource = dt; dv.DataSource = source; dr.Close(); conn.Close(); liste.Items.Add("Tout"); liste.Items.Add("Commencent par"); liste.Items.Add("Ne Commencent pas par"); liste.Items.Add("Se terminent par"); liste.Items.Add("Ne Se terminent pas par"); liste.Items.Add("Contiennent "); liste.Items.Add("ne Contiennent pas"); liste.SelectedIndex = 0; } private void filtrer() { DataView fl = new DataView(dt); switch (this.liste.SelectedIndex) { case 0: fl.RowFilter = "Nom like '%'"; rechercher.Text = ""; break; case 1: // begin fl.RowFilter = "Nom like '" + rechercher.Text.Trim() + "%'"; break; case 2: // not begin fl.RowFilter = "Nom not like '" + rechercher.Text.Trim() + "%'"; break; case 3://se termine fl.RowFilter = "Nom like '%" + rechercher.Text.Trim() + "'"; break; case 4: //Not ends with fl.RowFilter = "Nom not like '%" + rechercher.Text.Trim() + "'"; break; case 5:// contient fl.RowFilter = "Nom like '%" + rechercher.Text.Trim() + "%'"; break; case 6:// ne contient pas fl.RowFilter = "Nom not like '%" + rechercher.Text.Trim() + "%'"; break; } this.dv.DataSource = fl; } private void button1_Click(object sender, EventArgs e) { filtrer(); } private void rechercher_TextChanged(object sender, EventArgs e) { filtrer(); } } }