} // Constructor // Add Matiere Suivie public void addMS(SuitMatiere MS) { using (var dbConnection = new SqlConnection(InscriptionConnectionString)) { dbConnection.Open(); // Execute SQL Statements using (SqlCommand sqlCommand = dbConnection.CreateCommand()) { var annee = MS.Annee; var idEtudiant = MS.IdEtudiant; var idMatiere = MS.IdMatiere; var sqlStatementTemplate = "INSERT INTO inscription.SuitMatiere ( Annee, IdEtudiant, IdMatiere ) VALUES ( N'{0}', N'{1}', N'{2}')"; var sqlStatement = String.Format( sqlStatementTemplate, annee, idEtudiant, idMatiere ); // Insert into DB sqlCommand.CommandText = sqlStatement; sqlCommand.ExecuteNonQuery(); } // SQL Command dbConnection.Close(); } // dbConnection } // Method
} // Method // Get Matiere Suivie public List<SuitMatiere> getMS( ) { List<SuitMatiere> matieresSuivies = null; // Connect to DB using (var dbConnection = new SqlConnection(InscriptionConnectionString)) { dbConnection.Open(); // Execute SQL Statements using (SqlCommand sqlCommand = dbConnection.CreateCommand()) { // Select ALL matieres suivies sqlCommand.CommandText = "SELECT * FROM inscription.SuitMatiere"; SqlDataReader reader = sqlCommand.ExecuteReader(); // crée la liste des matières matieresSuivies = new List<SuitMatiere>(); while (reader.Read()) { var matiere = new SuitMatiere { Annee = (String) reader["Annee"], IdEtudiant = (int) reader["IdEtudiant"], IdMatiere = (int) reader["IdMatiere"] }; matieresSuivies.Add(matiere); } // While reader } // SQL Command dbConnection.Close(); } // dbConnection return matieresSuivies; } // Method