public bool Create(Performer performer) { if (performer.Id == 0) { var sql = string.Format("INSERT INTO Performers " + "(Name, Surname, Patronymic) VALUES(@Name, @Surname, @Patronymic)"); using (var connection = new SqlConnection()) { connection.ConnectionString = ConnectionString; connection.Open(); using (var command = new SqlCommand(sql, connection)) { command.Parameters.AddWithValue("@Name", performer.Name); command.Parameters.AddWithValue("@Surname", performer.Surname); command.Parameters.AddWithValue("@Patronymic", performer.Patronymic); command.ExecuteNonQuery(); } connection.Close(); } return true; } return false; }
public IEnumerable<Performer> GetAllPerformers() { var allPerformers = new List<Performer>(); using (var connection = new SqlConnection()) { connection.ConnectionString = ConnectionString; connection.Open(); const string strSql = "SELECT * FROM Performers"; var command = new SqlCommand(strSql, connection); var dataReader = command.ExecuteReader(CommandBehavior.CloseConnection); if (dataReader.HasRows) { while (dataReader.Read()) { var performer = new Performer { Id = dataReader.GetInt32(0), Name = dataReader.GetString(1), Surname = dataReader.GetString(2), Patronymic = dataReader.GetString(3) }; allPerformers.Add(performer); } } else { connection.Close(); return null; } connection.Close(); } return allPerformers; }