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
58 lines (51 loc) · 1.92 KB
/
Copy pathProgram.cs
File metadata and controls
58 lines (51 loc) · 1.92 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
using System;
using Microsoft.Data.Sqlite;
namespace InMemorySample
{
class Program
{
static void Main()
{
// Using a name and a shared cache allows multiple connections to access the same
// in-memory database
const string connectionString = "Data Source=InMemorySample;Mode=Memory;Cache=Shared";
// The in-memory database only persists while a connection is open to it. To manage
// its lifetime, keep one open connection around for as long as you need it.
var masterConnection = new SqliteConnection(connectionString);
masterConnection.Open();
var createCommand = masterConnection.CreateCommand();
createCommand.CommandText =
@"
CREATE TABLE data (
value TEXT
)
";
createCommand.ExecuteNonQuery();
using (var firstConnection = new SqliteConnection(connectionString))
{
firstConnection.Open();
var updateCommand = firstConnection.CreateCommand();
updateCommand.CommandText =
@"
INSERT INTO data
VALUES ('Hello, memory!')
";
updateCommand.ExecuteNonQuery();
}
using (var secondConnection = new SqliteConnection(connectionString))
{
secondConnection.Open();
var queryCommand = secondConnection.CreateCommand();
queryCommand.CommandText =
@"
SELECT *
FROM data
";
var value = (string)queryCommand.ExecuteScalar();
Console.WriteLine(value);
}
// After all the connections are closed, the database is deleted.
masterConnection.Close();
}
}
}