forked from dotnet/efcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
57 lines (49 loc) · 1.79 KB
/
Copy pathProgram.cs
File metadata and controls
57 lines (49 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Data.Sqlite;
namespace AsyncSample
{
class Program
{
static async Task Main()
{
var connection = new SqliteConnection("Data Source=AsyncSample.db");
connection.Open();
var createCommand = connection.CreateCommand();
createCommand.CommandText =
@"
CREATE TABLE data (
value BLOB
)
";
createCommand.ExecuteNonQuery();
// 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.
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");
}
}
}