|
1 | 1 | using System;
|
| 2 | +using System.Data; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Microsoft.Data.SqlClient; |
2 | 6 |
|
3 | 7 | namespace SlowSqlDataReaderDemo
|
4 | 8 | {
|
5 | 9 | class Program
|
6 | 10 | {
|
7 |
| - static void Main(string[] args) |
| 11 | + static string connectionString = "Server=localhost;Integrated Security=True;Database=VssfSdkSample_Configuration"; |
| 12 | + |
| 13 | + static async Task Main(string[] args) |
| 14 | + { |
| 15 | + PrintHelp(); |
| 16 | + Console.Write("> "); |
| 17 | + string input = Console.ReadLine(); |
| 18 | + while (input != "exit") |
| 19 | + { |
| 20 | + if (!string.IsNullOrWhiteSpace(input)) |
| 21 | + { |
| 22 | + string[] parts = input.Split(' ', StringSplitOptions.RemoveEmptyEntries); |
| 23 | + switch (parts[0]) |
| 24 | + { |
| 25 | + case "help": |
| 26 | + PrintHelp(); |
| 27 | + break; |
| 28 | + |
| 29 | + case "write": |
| 30 | + if (int.TryParse(parts[1], out int n)) |
| 31 | + { |
| 32 | + Stopwatch stopwatch = Stopwatch.StartNew(); |
| 33 | + string longString = new string('x', n); |
| 34 | + string command = $"update tbl_RegistryItems set RegValue='{longString}' where ParentPath = 'SqlDataReader'"; |
| 35 | + await ExecuteNonQueryAsync(command); |
| 36 | + Console.WriteLine($"Wrote {n} characters in {stopwatch.ElapsedMilliseconds} ms"); |
| 37 | + } |
| 38 | + break; |
| 39 | + |
| 40 | + case "read": |
| 41 | + { |
| 42 | + Stopwatch stopwatch = Stopwatch.StartNew(); |
| 43 | + int charactersRead = 0; |
| 44 | + string command = "select RegValue from tbl_RegistryItems where ParentPath = 'SqlDataReader'"; |
| 45 | + if (parts[1] == "sync") |
| 46 | + { |
| 47 | + charactersRead = Read(command); |
| 48 | + } |
| 49 | + else if (parts[1] == "async") |
| 50 | + { |
| 51 | + charactersRead = await ReadAsync(command, parts.Length > 2 && parts[2] == "sa"); |
| 52 | + } |
| 53 | + Console.WriteLine($"Read {charactersRead} characters in {stopwatch.ElapsedMilliseconds} ms"); |
| 54 | + } |
| 55 | + break; |
| 56 | + |
| 57 | + default: |
| 58 | + Console.WriteLine($"Unrecognized command: {input}"); |
| 59 | + break; |
| 60 | + } |
| 61 | + } |
| 62 | + Console.Write("> "); |
| 63 | + input = Console.ReadLine(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private static int Read(string s) |
| 68 | + { |
| 69 | + int numCharactersTransferred = 0; |
| 70 | + using (SqlConnection connection = new SqlConnection(connectionString)) |
| 71 | + { |
| 72 | + SqlCommand command = new SqlCommand(s, connection); |
| 73 | + connection.Open(); |
| 74 | + using (SqlDataReader reader = command.ExecuteReader()) |
| 75 | + { |
| 76 | + while (reader.Read()) |
| 77 | + { |
| 78 | + numCharactersTransferred += reader[0].ToString().Length; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + return numCharactersTransferred; |
| 83 | + } |
| 84 | + |
| 85 | + private static async Task<int> ReadAsync(string s, bool sequentialAccess) |
| 86 | + { |
| 87 | + int numCharactersTransferred = 0; |
| 88 | + using (SqlConnection connection = new SqlConnection(connectionString)) |
| 89 | + { |
| 90 | + SqlCommand command = new SqlCommand(s, connection); |
| 91 | + await connection.OpenAsync(); |
| 92 | + using (SqlDataReader reader = await command.ExecuteReaderAsync(sequentialAccess ? CommandBehavior.SequentialAccess : CommandBehavior.Default)) |
| 93 | + { |
| 94 | + while (await reader.ReadAsync()) |
| 95 | + { |
| 96 | + numCharactersTransferred += reader[0].ToString().Length; |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + return numCharactersTransferred; |
| 101 | + } |
| 102 | + |
| 103 | + private static async Task ExecuteNonQueryAsync(string s) |
| 104 | + { |
| 105 | + using (SqlConnection connection = new SqlConnection(connectionString)) |
| 106 | + { |
| 107 | + SqlCommand command = new SqlCommand(s, connection); |
| 108 | + await connection.OpenAsync(); |
| 109 | + await command.ExecuteNonQueryAsync(); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private static void PrintHelp() |
8 | 114 | {
|
9 |
| - Console.WriteLine("Hello World!"); |
| 115 | + Console.WriteLine("Slow SqlDataReader demo"); |
| 116 | + Console.WriteLine("Commands:"); |
| 117 | + Console.WriteLine(" exit"); |
| 118 | + Console.WriteLine(" help"); |
| 119 | + Console.WriteLine(" write <n>"); |
| 120 | + Console.WriteLine(" read async"); |
| 121 | + Console.WriteLine(" read sync"); |
| 122 | + Console.WriteLine(" read async sa (for sequential access)"); |
10 | 123 | }
|
11 | 124 | }
|
12 | 125 | }
|
0 commit comments