Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views4 pages

Database Helper - Cs

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

Database Helper - Cs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

using System;

using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

// App_Code/DatabaseHelper.cs
public class DatabaseHelper
{
private string connectionString;

public DatabaseHelper()
{
connectionString =
ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
}

// Récupérer tous les étudiants


public List<Student> GetAllStudents()
{
List<Student> students = new List<Student>();

using (SqlConnection connection = new SqlConnection(connectionString))


{
string query = "SELECT Id, Nom, Prenom, Email, Telephone,
DateNaissance, Filiere, DateInscription FROM Etudiants ORDER BY Nom";
SqlCommand command = new SqlCommand(query, connection);

connection.Open();
SqlDataReader reader = command.ExecuteReader();

while (reader.Read())
{
Student student = new Student
{
Id = Convert.ToInt32(reader["Id"]),
Nom = reader["Nom"].ToString(),
Prenom = reader["Prenom"].ToString(),
Email = reader["Email"].ToString(),
Telephone = reader["Telephone"].ToString(),
DateNaissance = reader["DateNaissance"] as DateTime?,
Filiere = reader["Filiere"].ToString(),
DateInscription = Convert.ToDateTime(reader["DateInscription"])
};
students.Add(student);
}
}

return students;
}

// Récupérer un étudiant par ID


public Student GetStudentById(int id)
{
Student student = null;

using (SqlConnection connection = new SqlConnection(connectionString))


{
string query = "SELECT Id, Nom, Prenom, Email, Telephone,
DateNaissance, Filiere, DateInscription FROM Etudiants WHERE Id = @Id";
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@Id", id);

connection.Open();
SqlDataReader reader = command.ExecuteReader();

if (reader.Read())
{
student = new Student
{
Id = Convert.ToInt32(reader["Id"]),
Nom = reader["Nom"].ToString(),
Prenom = reader["Prenom"].ToString(),
Email = reader["Email"].ToString(),
Telephone = reader["Telephone"].ToString(),
DateNaissance = reader["DateNaissance"] as DateTime?,
Filiere = reader["Filiere"].ToString(),
DateInscription = Convert.ToDateTime(reader["DateInscription"])
};
}
}

return student;
}

// Ajouter un étudiant
public bool AddStudent(Student student)
{
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
string query = @"INSERT INTO Etudiants (Nom, Prenom, Email,
Telephone, DateNaissance, Filiere)
VALUES (@Nom, @Prenom, @Email, @Telephone,
@DateNaissance, @Filiere)";
SqlCommand command = new SqlCommand(query, connection);

command.Parameters.AddWithValue("@Nom", student.Nom);
command.Parameters.AddWithValue("@Prenom", student.Prenom);
command.Parameters.AddWithValue("@Email", student.Email);
command.Parameters.AddWithValue("@Telephone", student.Telephone ??
(object)DBNull.Value);
command.Parameters.AddWithValue("@DateNaissance",
student.DateNaissance ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@Filiere", student.Filiere ??
(object)DBNull.Value);

connection.Open();
int result = command.ExecuteNonQuery();
return result > 0;
}
}
catch
{
return false;
}
}
// Modifier un étudiant
public bool UpdateStudent(Student student)
{
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
string query = @"UPDATE Etudiants SET Nom = @Nom, Prenom = @Prenom,
Email = @Email,
Telephone = @Telephone, DateNaissance =
@DateNaissance, Filiere = @Filiere
WHERE Id = @Id";
SqlCommand command = new SqlCommand(query, connection);

command.Parameters.AddWithValue("@Id", student.Id);
command.Parameters.AddWithValue("@Nom", student.Nom);
command.Parameters.AddWithValue("@Prenom", student.Prenom);
command.Parameters.AddWithValue("@Email", student.Email);
command.Parameters.AddWithValue("@Telephone", student.Telephone ??
(object)DBNull.Value);
command.Parameters.AddWithValue("@DateNaissance",
student.DateNaissance ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@Filiere", student.Filiere ??
(object)DBNull.Value);

connection.Open();
int result = command.ExecuteNonQuery();
return result > 0;
}
}
catch
{
return false;
}
}

// Supprimer un étudiant
public bool DeleteStudent(int id)
{
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
string query = "DELETE FROM Etudiants WHERE Id = @Id";
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@Id", id);

connection.Open();
int result = command.ExecuteNonQuery();
return result > 0;
}
}
catch
{
return false;
}
}
// Vérifier si un email existe déjà
public bool EmailExists(string email, int excludeId = 0)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
string query = "SELECT COUNT(*) FROM Etudiants WHERE Email = @Email AND
Id != @ExcludeId";
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@Email", email);
command.Parameters.AddWithValue("@ExcludeId", excludeId);

connection.Open();
int count = Convert.ToInt32(command.ExecuteScalar());
return count > 0;
}
}
}

You might also like