|
1173 | 1173 | 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.
|
1174 | 1174 | </para>
|
1175 | 1175 | <!-- 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> |
1177 | 1276 | </example>
|
1178 | 1277 | <exception cref="T:System.InvalidCastException">
|
1179 | 1278 | <list type="bullet">
|
|
0 commit comments