Mar.27
TP16 : Dataset typé
Objectif
Dans cet atelier vous allez apprendre à utiliser correctement les liaisons de données (databinding) et un dataset typé.
Questions
Partie serveur :
- Dans la base bibliotheque, crée les tables :
Thème (CodeTh, IntituléTh).
Livre (CodeL, Titre, Auteur, NbExemplaires, #CodeTh).
Adhérent (CodeA, NomA, Adresse, DateInscription).
- Remplir les tables.
Partie client :
- Réaliser un menu qui facilite l’utilisation de l’application.
- Créer l’interface suivante :

- Créer un DataSet typé dsBiblio (Voir l’annexe pour un exemple).
- Associer la table Adhérent aux champs de texte respectifs à l’aide des DataBindings.
- Écrire le code correspondant à chaque bouton réalisant le traitement attendu en utilisant les objets crées.
- Refaire le même travail pour la mise à jour de thème et livre :


Annexe : Exemple de création d’un dataSet Typé :
- Menu data à Add puis New data source:

- Cliquer sur le bouton suivant :

- Cliquer sur le bouton suivant :

- Choisir une nouvelle connexion :

- Cliquer sur suivant :

Correction
La classe Theme
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 TP17_Biblio__2
{
public partial class Theme : Form
{
static SqlConnection con = new SqlConnection("Data Source=DESKTOP-EIALG0J\\SQLEXPRESS ;Initial Catalog=bibliotheque;Integrated Security=True");
bibliothequeDataSet ds = new bibliothequeDataSet();
static SqlDataAdapter da = new SqlDataAdapter("select * from Theme", con);
SqlCommandBuilder build = new SqlCommandBuilder(da);
public Theme()
{
InitializeComponent();
}
private void Theme_Load(object sender, EventArgs e)
{
da.Fill(ds.Theme);
da.FillSchema(ds, SchemaType.Mapped, ds.Theme.ToString());
codeT.DataBindings.Add("text", ds.Theme, ds.Theme.CodeThemeColumn.ToString());
intitule.DataBindings.Add("text", ds.Theme, ds.Theme.IntituleThemeColumn.ToString());
}
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text != "Ajouter"){
BindingContext[ds.Theme].AddNew();
button1.Text = "Ajouter";
}
else {
BindingContext[ds.Theme].EndCurrentEdit();
da.Update(ds.Theme.GetChanges());
ds.AcceptChanges();
button1.Text = "Nouveau";
}
}
private void button2_Click(object sender, EventArgs e)
{
BindingContext[ds.Theme].EndCurrentEdit();
da.Update(ds.Theme.GetChanges());
ds.AcceptChanges();
}
private void button3_Click(object sender, EventArgs e)
{
BindingContext[ds.Theme].RemoveAt(BindingContext[ds.Theme].Position);
da.Update(ds.Theme.GetChanges(DataRowState.Deleted));
ds.AcceptChanges();
}
private void button9_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button6_Click(object sender, EventArgs e)
{
BindingContext[ds.Theme].Position = 0;
}
private void button8_Click(object sender, EventArgs e)
{
BindingContext[ds.Theme].Position += 1;
}
private void button5_Click(object sender, EventArgs e)
{
BindingContext[ds.Theme].Position -= 1;
}
private void button7_Click(object sender, EventArgs e)
{
BindingContext[ds.Theme].Position = ds.Theme.Rows.Count - 1;
}
private void button4_Click(object sender, EventArgs e)
{
int x = int.Parse(codeR.Text.ToString());
BindingContext[ds.Theme].Position = x - 1;
}
}
}
La classe Adherent
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 TP17_Biblio__2
{
public partial class Adherent : Form
{
static SqlConnection con = new SqlConnection("Data Source=DESKTOP-EIALG0J\\SQLEXPRESS ;Initial Catalog=bibliotheque;Integrated Security=True");
bibliothequeDataSet ds = new bibliothequeDataSet();
static SqlDataAdapter da = new SqlDataAdapter("select * from Adherent", con);
SqlCommandBuilder build = new SqlCommandBuilder(da);
public Adherent()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text != "Ajouter"){
BindingContext[ds.Adherent].AddNew();
button1.Text = "Ajouter";
}
else {
BindingContext[ds.Adherent].EndCurrentEdit();
da.Update(ds.Adherent.GetChanges());
ds.AcceptChanges();
button1.Text = "Nouveau";
}
}
private void button2_Click(object sender, EventArgs e)
{
BindingContext[ds.Adherent].EndCurrentEdit();
da.Update(ds.Adherent.GetChanges());
ds.AcceptChanges();
}
private void button3_Click(object sender, EventArgs e)
{
BindingContext[ds.Adherent].RemoveAt(BindingContext[ds.Adherent].Position);
da.Update(ds.Adherent.GetChanges(DataRowState.Deleted));
ds.AcceptChanges();
}
private void button9_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void Theme_Load(object sender, EventArgs e)
{
da.Fill(ds.Adherent);
da.FillSchema(ds, SchemaType.Mapped, ds.Adherent.ToString());
codeA.DataBindings.Add("text", ds.Adherent, ds.Adherent.CodeAdherentColumn.ToString());
nomA.DataBindings.Add("text", ds.Adherent, ds.Adherent.NomAdherentColumn.ToString());
adresseA.DataBindings.Add("text", ds.Adherent, ds.Adherent.AdresseColumn.ToString());
dateA.DataBindings.Add("value", ds.Adherent, ds.Adherent.DateInscriptionColumn.ToString());
}
private void button6_Click(object sender, EventArgs e)
{
BindingContext[ds.Adherent].Position = 0;
}
private void button8_Click(object sender, EventArgs e)
{
BindingContext[ds.Adherent].Position += 1;
}
private void button5_Click(object sender, EventArgs e)
{
BindingContext[ds.Adherent].Position -= 1;
}
private void button7_Click(object sender, EventArgs e)
{
BindingContext[ds.Adherent].Position = ds.Adherent.Rows.Count - 1;
}
private void button4_Click(object sender, EventArgs e)
{
int x=int.Parse(codeR.Text.ToString());
BindingContext[ds.Adherent].Position = x-1;
}
}
}
