public class UserDAO { private readonly string _connectionString; public UserDAO(string connectionString) { _connectionString = connectionString; } public User GetUserById(int id) { using (var connection = new SqlConnection(_connectionString)) using (var command = new SqlCommand("SELECT * FROM Users WHERE Id = @id", connection)) { command.Parameters.AddWithValue("@id", id); connection.Open(); using (var reader = command.ExecuteReader()) { if (!reader.Read()) { return null; } return new User { Id = reader.GetInt32(reader.GetOrdinal("Id")), Name = reader.GetString(reader.GetOrdinal("Name")), Email = reader.GetString(reader.GetOrdinal("Email")) }; } } } }
public class UserDAO { private readonly string _connectionString; public UserDAO(string connectionString) { _connectionString = connectionString; } public void InsertUser(User user) { using (var connection = new SqlConnection(_connectionString)) using (var command = new SqlCommand("INSERT INTO Users (Name, Email) VALUES (@name, @email)", connection)) { command.Parameters.AddWithValue("@name", user.Name); command.Parameters.AddWithValue("@email", user.Email); connection.Open(); command.ExecuteNonQuery(); } } }These examples make use of the SqlConnection and SqlCommand classes to work with the database. The package library used is likely to be the System.Data.SqlClient package.