// 2. АСИНХРОННОЕ ЧТЕНИЕ КНИГ
static async Task ReadBooksAsync()
{
string connectionString = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Library;Integrated Security=True;";
string sql = "SELECT Id, Title, Price, Pages, AuthorId FROM Books";
using (SqlConnection conn = new SqlConnection(connectionString))
{
await conn.OpenAsync();
using (SqlCommand cmd = new SqlCommand(sql, conn))
using (SqlDataReader reader = await cmd.ExecuteReaderAsync())
{
while (await reader.ReadAsync())
{
int id = reader.GetInt32(0);
string title = reader.GetString(1);
decimal price = reader.GetDecimal(2);
int pages = reader.GetInt32(3);
int authorId = reader.GetInt32(4);
Console.WriteLine($"{id}. {title} | Цена: {price} руб | Страниц: {pages} | Автор: {authorId}");
}
}
}
}