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
68 lines (57 loc) · 1.99 KB
/
Copy pathProgram.cs
File metadata and controls
68 lines (57 loc) · 1.99 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
using System;
using Microsoft.Data.Sqlite;
namespace BatchingSample
{
class Program
{
static void Main()
{
var connection = new SqliteConnection("Data Source=:memory:");
connection.Open();
// SQLite doesn't support batching natively. Since there's no network involved, it
// wouldn't really help with performance anyway. Batching is implemented in
// Microsoft.Data.Sqlite as a convenience. For better command performance, see
// BulkInsertSample.
var createCommand = connection.CreateCommand();
createCommand.CommandText =
@"
CREATE TABLE blog (
id INTEGER PRIMARY KEY,
name TEXT
);
CREATE TABLE post (
id INTEGER PRIMARY KEY,
title TEXT,
blog_id INTEGER NOT NULL,
FOREIGN KEY (blog_id) REFERENCES blog
);
INSERT INTO blog
VALUES (1, 'Brice''s Blog');
INSERT INTO post
VALUES (1, 'Hello, World!', 1),
(2, 'SQLite on .NET Core', 1);
";
createCommand.ExecuteNonQuery();
var queryCommand = connection.CreateCommand();
queryCommand.CommandText =
@"
SELECT *
FROM blog;
SELECT *
FROM post;
";
var reader = queryCommand.ExecuteReader();
// Read the first result set
while (reader.Read())
{
Console.WriteLine($"Blog {reader["id"]}: {reader["name"]}");
}
// Read the second result set
reader.NextResult();
while (reader.Read())
{
Console.WriteLine($"Post {reader["id"]} in blog {reader["blog_id"]}: {reader["title"]}");
}
}
}
}