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
51 lines (45 loc) · 1.47 KB
/
Copy pathProgram.cs
File metadata and controls
51 lines (45 loc) · 1.47 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
using System;
using Microsoft.Data.Sqlite;
namespace ScalarFunctionSample
{
class Program
{
static void Main()
{
var connection = new SqliteConnection("Data Source=:memory:");
connection.Open();
var createCommand = connection.CreateCommand();
createCommand.CommandText =
@"
CREATE TABLE cylinder (
name TEXT,
radius REAL,
height REAL
);
INSERT INTO cylinder
VALUES ('1x2', 1.0, 2.0),
('2x1', 2.0, 1.0);
";
createCommand.ExecuteNonQuery();
// SQLite will invoke this managed delegate. Debug it like you would any other code
// by setting breakpoints, etc.
connection.CreateFunction(
"volume",
(double radius, double height)
=> Math.PI * Math.Pow(radius, 2) * height);
var queryCommand = connection.CreateCommand();
queryCommand.CommandText =
@"
SELECT name,
volume(radius, height) AS volume
FROM cylinder
ORDER BY volume DESC
";
var reader = queryCommand.ExecuteReader();
while (reader.Read())
{
Console.WriteLine($"{reader["name"]}: {reader["volume"]}");
}
}
}
}