Skip to content

Commit fbd53a1

Browse files
authored
feature: Example of adding and querying data. (#4)
* feature: Example of adding and querying data.
1 parent 832246f commit fbd53a1

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

example/GetStarted/Program.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,34 @@
1414

1515
Console.WriteLine($@"Connection state: {conn.State}");
1616

17+
{
18+
await TestScalar();
19+
await TestReader();
20+
}
21+
22+
Console.WriteLine(@"Completed!");
23+
24+
25+
async Task TestScalar()
1726
{
1827
await using var cmd = new NpgsqlCommand("SELECT 1", conn);
1928
var result = await cmd.ExecuteScalarAsync();
2029
Console.WriteLine(result);
2130
}
2231

23-
Console.WriteLine(@"Completed!");
32+
async Task TestReader()
33+
{
34+
await using (var cmd = new NpgsqlCommand("SELECT * FROM employees", conn))
35+
{
36+
await using var reader = await cmd.ExecuteReaderAsync();
37+
while (await reader.ReadAsync())
38+
{
39+
// 读取每一行数据
40+
var id = reader.GetInt32(0); // 假设第一列是ID,类型为int
41+
var name = reader.GetString(1); // 假设第二列是Name,类型为varchar
42+
var age = reader.GetInt32(2); // 假设第三列是Age,可为空
43+
44+
Console.WriteLine($"ID: {id}, Name: {name}, Age: {age}");
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)