Skip to content

Commit 5a24c86

Browse files
authored
Change sample codes to call Close method when done reading. (#8438)
1 parent c417f14 commit 5a24c86

File tree

9 files changed

+87
-13
lines changed

9 files changed

+87
-13
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>net6.0</TargetFrameworks>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="System.Data.SqlClient" Version="4.8.3" />
10+
</ItemGroup>
11+
12+
</Project>

snippets/csharp/VS_Snippets_ADO.NET/Classic WebData SqlCommand.ExecuteReader Example/CS/source.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ private static void CreateCommand(string queryString,
2323
connection.Open();
2424

2525
SqlCommand command = new SqlCommand(queryString, connection);
26-
SqlDataReader reader = command.ExecuteReader();
27-
while (reader.Read())
26+
using(SqlDataReader reader = command.ExecuteReader())
2827
{
29-
Console.WriteLine(String.Format("{0}", reader[0]));
28+
while (reader.Read())
29+
{
30+
Console.WriteLine(String.Format("{0}", reader[0]));
31+
}
3032
}
3133
}
3234
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>net6.0</TargetFrameworks>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="System.Data.SqlClient" Version="4.8.3" />
10+
</ItemGroup>
11+
12+
</Project>

snippets/csharp/VS_Snippets_ADO.NET/Classic WebData SqlCommand.ExecuteReader2/CS/mysample.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ private static void CreateCommand(string queryString,
2222
{
2323
SqlCommand command = new SqlCommand(queryString, connection);
2424
connection.Open();
25-
SqlDataReader reader =
26-
command.ExecuteReader(CommandBehavior.CloseConnection);
27-
while (reader.Read())
25+
using(SqlDataReader reader =
26+
command.ExecuteReader(CommandBehavior.CloseConnection))
2827
{
29-
Console.WriteLine(String.Format("{0}", reader[0]));
28+
while (reader.Read())
29+
{
30+
Console.WriteLine(String.Format("{0}", reader[0]));
31+
}
3032
}
3133
}
3234
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>net6.0</TargetFrameworks>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="System.Data.SqlClient" Version="4.8.3" />
10+
</ItemGroup>
11+
12+
</Project>

snippets/visualbasic/VS_Snippets_ADO.NET/Classic WebData SqlCommand.ExecuteReader Example/VB/source.vb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@ Module Module1
2121

2222
Dim command As New SqlCommand(queryString, connection)
2323
Dim reader As SqlDataReader = command.ExecuteReader()
24-
While reader.Read()
25-
Console.WriteLine("{0}", reader(0))
26-
End While
24+
Try
25+
While reader.Read()
26+
Console.WriteLine("{0}", reader(0))
27+
End While
28+
Finally
29+
' Always call Close when done reading.
30+
reader.Close()
31+
End Try
2732
End Using
2833
End Sub
2934
' </Snippet1>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>net6.0</TargetFrameworks>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="System.Data.SqlClient" Version="4.8.3" />
10+
</ItemGroup>
11+
12+
</Project>

snippets/visualbasic/VS_Snippets_ADO.NET/Classic WebData SqlCommand.ExecuteReader2/VB/mysample.vb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@ Module Module1
2121
connection.Open()
2222
Dim reader As SqlDataReader = _
2323
command.ExecuteReader(CommandBehavior.CloseConnection)
24-
While reader.Read()
25-
Console.WriteLine("{0}", reader(0))
26-
End While
24+
Try
25+
While reader.Read()
26+
Console.WriteLine("{0}", reader(0))
27+
End While
28+
Finally
29+
' Always call Close when done reading.
30+
reader.Close()
31+
End Try
2732
End Using
2833
End Sub
2934
' </Snippet1>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>net6.0</TargetFrameworks>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="System.Data.SqlClient" Version="4.8.3" />
10+
</ItemGroup>
11+
12+
</Project>

0 commit comments

Comments
 (0)