File tree Expand file tree Collapse file tree 1 file changed +25
-1
lines changed
Expand file tree Collapse file tree 1 file changed +25
-1
lines changed Original file line number Diff line number Diff line change 1414
1515Console . 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+ }
You can’t perform that action at this time.
0 commit comments