-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathProgram.cs
69 lines (55 loc) · 1.88 KB
/
Program.cs
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
var sqlInstance = new SqlInstance(
name: "DeltaWebApplication",
buildTemplate: DbBuilder.Create);
await using var database = await sqlInstance.Build("WebApp");
var connectionString = database.ConnectionString;
#region UseDeltaSqlServer
var builder = WebApplication.CreateBuilder();
builder.Services.AddScoped(_ => new SqlConnection(connectionString));
var app = builder.Build();
app.UseDelta();
#endregion
await using var command = database.Connection.CreateCommand();
command.CommandText =
$"""
insert into [Companies] (Id, Content)
values ('{Guid.NewGuid()}', 'The company')
""";
await command.ExecuteNonQueryAsync();
app.MapGet(
"/",
async _ =>
{
var connection = _.RequestServices.GetRequiredService<SqlConnection>();
if (connection.State == ConnectionState.Closed)
{
await connection.OpenAsync();
}
var lastTimeStamp = await connection.GetLastTimeStamp();
await using var command = connection.CreateCommand();
command.CommandText = "select * from Companies";
await using var reader = await command.ExecuteReaderAsync();
var builder = new StringBuilder("Results: ");
builder.AppendLine();
builder.AppendLine($"LastTimeStamp: {lastTimeStamp}");
builder.AppendLine();
while (await reader.ReadAsync())
{
for (var i = 0; i < reader.FieldCount; i++)
{
var value = reader.GetValue(i);
if (value is byte[] bytes)
{
value = BitConverter.ToString(bytes);
}
builder.AppendLine($"{reader.GetName(i)}: {value}");
}
}
await _.Response.WriteAsync(builder.ToString());
});
#region UseDeltaMapGroup
app.MapGroup("/group")
.UseDelta()
.MapGet("/", () => "Hello Group!");
#endregion
app.Run();