Exploitation d’une base de données en mode connecté - c# - d

ADO.NET Exploitattion d’une base en mode connecté Visual C#-SQL Server 2000 L. Hassouni 1 ADO.NET Mode Connecté Objet : Exploitation d’une base de données en mode connecté Objets utilisés : SqlConnection, SqlCommand, SqlDataReader Fournisseur : SQL Server Base de données : JD Notation Concevez une application qui permet d’effectuer en mode connecté les opérations suivantes :  Consultation d’enregistrement  Ajout de nouveaux enregistrements  Modification d’enregistrments  Suppression d’enregistrements Nous vous proposons l’interface ci-dessous : ADO.NET Exploitattion d’une base en mode connecté Visual C#-SQL Server 2000 L. Hassouni 2 Correction using System.Data; using System.Data.SqlClient; ............................................................................... /// Variable nécessaire au concepteur. static SqlConnection MySqlConn; static SqlCommand MySqlCmd; static SqlDataReader MySqlDataRead; static string MyStrConn; static string MyStrSql; static int NumEnreg, NbEnreg; --------------------------------- private void BtnConnexion_Click(object sender, System.EventArgs e) { MyStrConn = "Server = localhost; Integrated security = SSPI; Initial Catalog = SQLJDNotation"; MySqlConn = new SqlConnection(MyStrConn); MySqlConn.Open(); } ----------------------------------------------------------------------- private void BtnLecture_Click(object sender, System.EventArgs e) { MyStrSql = "SELECT COUNT(*) FROM Etudiants"; MySqlCmd = new SqlCommand(MyStrSql, MySqlConn); NumEnreg = 0; NbEnreg = (int)MySqlCmd.ExecuteScalar(); MyStrSql = "SELECT * FROM Etudiants"; MySqlCmd = new SqlCommand(MyStrSql, MySqlConn); MySqlDataRead = MySqlCmd.ExecuteReader(); If (MySqlDataRead.HasRows) {

MySqlDataRead.Read();

TxtNumEtud.Text = MySqlDataRead.GetInt16(0).ToString();

TxtNom.Text = MySqlDataRead.GetString(1);

TxtPrenom.Text = MySqlDataRead.GetString(2);

TxtNumNbEnreg.Text = (NumEnreg+1).ToString()+"/"+NbEnreg.ToString();

NumEnreg++; } } -------------------------------------------------------------------------------------- private void BtnSuivant_Click(object sender, System.EventArgs e) { if (!MySqlDataRead.IsClosed) {

if (MySqlDataRead.Read()){ TxtNumEtud.Text = MySqlDataRead.GetInt16(0).ToString();

TxtNom.Text = MySqlDataRead.GetString(1);

TxtPrenom.Text = MySqlDataRead.GetString(2);

TxtNumNbEnreg.Text = (NumEnreg+1).ToString()+"/"+NbEnreg.ToString();

NumEnreg++;} else

MySqlDataRead.Close(); } } ----------------------------------------------------------------------------------------- private void BtnAjouter_Click(object sender, System.EventArgs e) { int EnregAffectes; if (!MySqlDataRead.IsClosed)

MySqlDataRead.Close(); MyStrSql = "INSERT INTO Etudiants (NumEtudiant, Nom, Prénom) Values(16, 'El Mejor', 'Najib')"; MySqlCmd = new SqlCommand(MyStrSql, MySqlConn); ADO.NET Exploitattion d’une base en mode connecté Visual C#-SQL Server 2000 L. Hassouni 3 EnregAffectes = (int)MySqlCmd.ExecuteNonQuery(); MessageBox.Show(EnregAffectes.ToString()); } ------------------------------------------------------------------------------------------ private void BtnModifier_Click(object sender, System.EventArgs e) { int EnregAffectes; if (!MySqlDataRead.IsClosed)

MySqlDataRead.Close(); MyStrSql = "UPDATE Etudiants SET Nom = 'EL LOCO', Prénom = 'Najia' WHERE NumEtudiant = 16"; MySqlCmd = new SqlCommand(MyStrSql, MySqlConn); EnregAffectes = (int)MySqlCmd.ExecuteNonQuery(); MessageBox.Show(EnregAffectes.ToString()); } --------------------------------------------------------------------------------------------- private void BtnSupprimer_Click(object sender, System.EventArgs e) { int EnregAffectes; if (!MySqlDataRead.IsClosed)

MySqlDataRead.Close(); MyStrSql = "DELETE FROM Etudiants WHERE NumEtudiant = 16"; MySqlCmd = new SqlCommand(MyStrSql, MySqlConn); EnregAffectes = (int)MySqlCmd.ExecuteNonQuery(); MessageBox.Show(EnregAffectes.ToString()); } ------------------------------------------------------------------------------------------------- private void BtnDeconnexion_Click(object sender, System.EventArgs e) { MySqlConn.Close(); }