/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
samples/snippets/standard/data/sqlite/AsyncSample/Program.cs
67 строк
2 KB
compujuckel
sqlite: do not recommend usage of shared cache (#35584)
06 июн 2023, 19:34
Не верифицирован
06 июн 2023, 19:34
ef04c3c
Код
Авторство
О чём код?
using System; using System.Diagnostics; using System.IO; using System.Threading.Tasks; using Microsoft.Data.Sqlite; namespace AsyncSample { class Program { static async Task Main() { // SQLite doesn't support asynchronous I/O. Instead, they recommend using a // write-ahead log (WAL) which improves write performance. This sample // demonstrates the anti-pattern of using ADO.NET's async methods with // Microsoft.Data.Sqlite. #region snippet_WAL var connection = new SqliteConnection("Data Source=AsyncSample.db"); connection.Open(); // Enable write-ahead logging var walCommand = connection.CreateCommand(); walCommand.CommandText = @" PRAGMA journal_mode = 'wal' "; walCommand.ExecuteNonQuery(); #endregion var createCommand = connection.CreateCommand(); createCommand.CommandText = @" CREATE TABLE data ( value BLOB ) "; createCommand.ExecuteNonQuery(); var insertCommand = connection.CreateCommand(); insertCommand.CommandText = @" INSERT INTO data VALUES ($value) "; Console.WriteLine("Generating 100 MB of data..."); var value = new byte[100_000_000]; var random = new Random(); random.NextBytes(value); insertCommand.Parameters.AddWithValue("$value", value); Console.WriteLine("Inserting data..."); var stopwatch = Stopwatch.StartNew(); var task = insertCommand.ExecuteNonQueryAsync(); Console.WriteLine($"Blocked for {stopwatch.ElapsedMilliseconds} ms"); stopwatch.Restart(); await task; Console.WriteLine($"Yielded for {stopwatch.ElapsedMilliseconds} ms"); // Clean up connection.Close(); File.Delete("AsyncSample.db"); } } }