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
85 lines (76 loc) · 3.17 KB
/
Copy pathProgram.cs
File metadata and controls
85 lines (76 loc) · 3.17 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Data.Sqlite;
namespace StreamingSample
{
class Program
{
static async Task Main()
{
var connection = new SqliteConnection("Data Source=StreamingSample.db");
connection.Open();
var createCommand = connection.CreateCommand();
createCommand.CommandText =
@"
CREATE TABLE data (
id INTEGER PRIMARY KEY AUTOINCREMENT,
value BLOB
)
";
createCommand.ExecuteNonQuery();
// You can reduce memory usage while reading and writing large objects by streaming
// the data into and out of the database. This can be especially useful when
// parsing or transforming the data
using (var inputStream = File.OpenRead("input.txt"))
{
// Start by inserting a row as normal. Use the zeroblob() function to allocate
// space in the database for the large object. The last_insert_rowid() function
// provides a convenient way to get its rowid
var insertCommand = connection.CreateCommand();
insertCommand.CommandText =
@"
INSERT INTO data(value)
VALUES (zeroblob($length));
SELECT last_insert_rowid();
";
insertCommand.Parameters.AddWithValue("$length", inputStream.Length);
var rowid = (long)insertCommand.ExecuteScalar();
// After inserting the row, open a stream to write the large object
using (var writeStream = new SqliteBlob(connection, "data", "value", rowid))
{
Console.WriteLine("Writing the large object...");
// NB: Although SQLite doesn't support async, other types of streams do
await inputStream.CopyToAsync(writeStream);
}
}
using (var outputStream = Console.OpenStandardOutput())
{
// To stream the large object, you must select the rowid or one of its aliases as
// show here in addition to the large object's column. If you don't, the entire
// object will be loaded into memory
var selectCommand = connection.CreateCommand();
selectCommand.CommandText =
@"
SELECT id, value
FROM data
LIMIT 1
";
using (var reader = selectCommand.ExecuteReader())
{
while (reader.Read())
{
using (var readStream = reader.GetStream(1))
{
Console.WriteLine("Reading the large object...");
await readStream.CopyToAsync(outputStream);
}
}
}
}
// Clean up
connection.Close();
File.Delete("StreamingSample.db");
}
}
}