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
81 lines (69 loc) · 2.34 KB
/
Copy pathProgram.cs
File metadata and controls
81 lines (69 loc) · 2.34 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
using System;
using System.Data;
using System.Data.Common;
using Microsoft.Data.Sqlite;
namespace ResultMetadataSample
{
class Program
{
static void Main()
{
var connection = new SqliteConnection("Data Source=:memory:");
connection.Open();
var createCommand = connection.CreateCommand();
createCommand.CommandText =
@"
CREATE TABLE post (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL UNIQUE,
body TEXT
);
";
createCommand.ExecuteNonQuery();
var queryCommand = connection.CreateCommand();
queryCommand.CommandText =
@"
SELECT id AS post_id,
title,
body,
random() AS random
FROM post;
";
var reader = queryCommand.ExecuteReader();
var schemaTable = reader.GetSchemaTable();
foreach (DataRow column in schemaTable.Rows)
{
if ((bool)column[SchemaTableColumn.IsExpression])
{
Console.Write("(expression) ");
}
else
{
Console.Write($"{column[SchemaTableColumn.BaseColumnName]} ");
}
if ((bool)column[SchemaTableColumn.IsAliased])
{
Console.Write($"AS {column[SchemaTableColumn.ColumnName]} ");
}
Console.Write($"{column["DataTypeName"]} ");
if (column[SchemaTableColumn.AllowDBNull] as bool? == false)
{
Console.Write("NOT NULL ");
}
if (column[SchemaTableColumn.IsKey] as bool? == true)
{
Console.Write("PRIMARY KEY ");
}
if (column[SchemaTableOptionalColumn.IsAutoIncrement] as bool? == true)
{
Console.Write("AUTOINCREMENT ");
}
if (column[SchemaTableColumn.IsUnique] as bool? == true)
{
Console.Write("UNIQUE ");
}
Console.WriteLine();
}
}
}
}