Skip to content

Commit 7db7af1

Browse files
Add missing sample (#3009)
1 parent 8f5f0c5 commit 7db7af1

File tree

2 files changed

+200
-1
lines changed

2 files changed

+200
-1
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// <Snippet1>
2+
using System;
3+
using Microsoft.Data.SqlClient;
4+
5+
class Class1
6+
{
7+
static void Main()
8+
{
9+
// This is a simple example that demonstrates the usage of the
10+
// BeginExecuteReader functionality
11+
// The WAITFOR statement simply adds enough time to prove the
12+
// asynchronous nature of the command.
13+
string commandText =
14+
"WAITFOR DELAY '00:00:03';" +
15+
"SELECT LastName, FirstName FROM Person.Contact " +
16+
"WHERE LastName LIKE 'M%'";
17+
18+
RunCommandAsynchronously(commandText, GetConnectionString());
19+
20+
Console.WriteLine("Press ENTER to continue.");
21+
Console.ReadLine();
22+
}
23+
24+
private static void RunCommandAsynchronously(
25+
string commandText, string connectionString)
26+
{
27+
// Given command text and connection string, asynchronously execute
28+
// the specified command against the connection. For this example,
29+
// the code displays an indicator as it is working, verifying the
30+
// asynchronous behavior.
31+
using (SqlConnection connection = new SqlConnection(connectionString))
32+
{
33+
try
34+
{
35+
SqlCommand command = new SqlCommand(commandText, connection);
36+
37+
connection.Open();
38+
IAsyncResult result = command.BeginExecuteReader();
39+
40+
// Although it is not necessary, the following code
41+
// displays a counter in the console window, indicating that
42+
// the main thread is not blocked while awaiting the command
43+
// results.
44+
int count = 0;
45+
while (!result.IsCompleted)
46+
{
47+
count += 1;
48+
Console.WriteLine("Waiting ({0})", count);
49+
// Wait for 1/10 second, so the counter
50+
// does not consume all available resources
51+
// on the main thread.
52+
System.Threading.Thread.Sleep(100);
53+
}
54+
55+
using (SqlDataReader reader = command.EndExecuteReader(result))
56+
{
57+
DisplayResults(reader);
58+
}
59+
}
60+
catch (SqlException ex)
61+
{
62+
Console.WriteLine("Error ({0}): {1}", ex.Number, ex.Message);
63+
}
64+
catch (InvalidOperationException ex)
65+
{
66+
Console.WriteLine("Error: {0}", ex.Message);
67+
}
68+
catch (Exception ex)
69+
{
70+
// You might want to pass these errors
71+
// back out to the caller.
72+
Console.WriteLine("Error: {0}", ex.Message);
73+
}
74+
}
75+
}
76+
77+
private static void DisplayResults(SqlDataReader reader)
78+
{
79+
// Display the data within the reader.
80+
while (reader.Read())
81+
{
82+
// Display all the columns.
83+
for (int i = 0; i < reader.FieldCount; i++)
84+
{
85+
Console.Write("{0}\t", reader.GetValue(i));
86+
}
87+
Console.WriteLine();
88+
}
89+
}
90+
91+
private static string GetConnectionString()
92+
{
93+
// To avoid storing the connection string in your code,
94+
// you can retrieve it from a configuration file.
95+
96+
return "Data Source=(local);Integrated Security=true;" +
97+
"Initial Catalog=AdventureWorks";
98+
}
99+
}
100+
// </Snippet1>

doc/snippets/Microsoft.Data.SqlClient/SqlCommand.xml

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1173,7 +1173,106 @@
11731173
The following console application starts the process of retrieving a data reader asynchronously. While waiting for the results, this simple application sits in a loop, investigating the <see cref="P:System.IAsyncResult.IsCompleted" /> property value. As soon as the process has completed, the code retrieves the <see cref="T:Microsoft.Data.SqlClient.SqlDataReader" /> and displays its contents.
11741174
</para>
11751175
<!-- SqlCommand_BeginExecuteReader -->
1176-
<!-- SAMPLE MISSING -->
1176+
<code language="c#">
1177+
using System;
1178+
using Microsoft.Data.SqlClient;
1179+
1180+
class Class1
1181+
{
1182+
static void Main()
1183+
{
1184+
// This is a simple example that demonstrates the usage of the
1185+
// BeginExecuteReader functionality
1186+
// The WAITFOR statement simply adds enough time to prove the
1187+
// asynchronous nature of the command.
1188+
string commandText =
1189+
"WAITFOR DELAY '00:00:03';" +
1190+
"SELECT LastName, FirstName FROM Person.Contact " +
1191+
"WHERE LastName LIKE 'M%'";
1192+
1193+
RunCommandAsynchronously(commandText, GetConnectionString());
1194+
1195+
Console.WriteLine("Press ENTER to continue.");
1196+
Console.ReadLine();
1197+
}
1198+
1199+
private static void RunCommandAsynchronously(
1200+
string commandText, string connectionString)
1201+
{
1202+
// Given command text and connection string, asynchronously execute
1203+
// the specified command against the connection. For this example,
1204+
// the code displays an indicator as it is working, verifying the
1205+
// asynchronous behavior.
1206+
using (SqlConnection connection = new SqlConnection(connectionString))
1207+
{
1208+
try
1209+
{
1210+
SqlCommand command = new SqlCommand(commandText, connection);
1211+
1212+
connection.Open();
1213+
IAsyncResult result = command.BeginExecuteReader();
1214+
1215+
// Although it is not necessary, the following code
1216+
// displays a counter in the console window, indicating that
1217+
// the main thread is not blocked while awaiting the command
1218+
// results.
1219+
int count = 0;
1220+
while (!result.IsCompleted)
1221+
{
1222+
count += 1;
1223+
Console.WriteLine("Waiting ({0})", count);
1224+
// Wait for 1/10 second, so the counter
1225+
// does not consume all available resources
1226+
// on the main thread.
1227+
System.Threading.Thread.Sleep(100);
1228+
}
1229+
1230+
using (SqlDataReader reader = command.EndExecuteReader(result))
1231+
{
1232+
DisplayResults(reader);
1233+
}
1234+
}
1235+
catch (SqlException ex)
1236+
{
1237+
Console.WriteLine("Error ({0}): {1}", ex.Number, ex.Message);
1238+
}
1239+
catch (InvalidOperationException ex)
1240+
{
1241+
Console.WriteLine("Error: {0}", ex.Message);
1242+
}
1243+
catch (Exception ex)
1244+
{
1245+
// You might want to pass these errors
1246+
// back out to the caller.
1247+
Console.WriteLine("Error: {0}", ex.Message);
1248+
}
1249+
}
1250+
}
1251+
1252+
private static void DisplayResults(SqlDataReader reader)
1253+
{
1254+
// Display the data within the reader.
1255+
while (reader.Read())
1256+
{
1257+
// Display all the columns.
1258+
for (int i = 0; i < reader.FieldCount; i++)
1259+
{
1260+
Console.Write("{0}\t", reader.GetValue(i));
1261+
}
1262+
Console.WriteLine();
1263+
}
1264+
}
1265+
1266+
private static string GetConnectionString()
1267+
{
1268+
// To avoid storing the connection string in your code,
1269+
// you can retrieve it from a configuration file.
1270+
1271+
return "Data Source=(local);Integrated Security=true;" +
1272+
"Initial Catalog=AdventureWorks";
1273+
}
1274+
}
1275+
</code>
11771276
</example>
11781277
<exception cref="T:System.InvalidCastException">
11791278
<list type="bullet">

0 commit comments

Comments
 (0)