Skip to content

Commit cbdcca8

Browse files
committed
1 parent 15670a5 commit cbdcca8

File tree

3 files changed

+145
-2
lines changed

3 files changed

+145
-2
lines changed

Program.cs

+115-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,125 @@
11
using System;
2+
using System.Data;
3+
using System.Diagnostics;
4+
using System.Threading.Tasks;
5+
using Microsoft.Data.SqlClient;
26

37
namespace SlowSqlDataReaderDemo
48
{
59
class Program
610
{
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()
8114
{
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)");
10123
}
11124
}
12125
}

SlowSqlDataReaderDemo.csproj

+5
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,9 @@
55
<TargetFramework>netcoreapp3.1</TargetFramework>
66
</PropertyGroup>
77

8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.Data.SqlClient" Version="2.1.1" />
10+
<PackageReference Include="System.Data.SqlClient" Version="4.8.2" />
11+
</ItemGroup>
12+
813
</Project>

SlowSqlDataReaderDemo.sln

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29708.233
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SlowSqlDataReaderDemo", "SlowSqlDataReaderDemo.csproj", "{121675BF-0F4A-41A6-8B66-C931E0BC7E35}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{121675BF-0F4A-41A6-8B66-C931E0BC7E35}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{121675BF-0F4A-41A6-8B66-C931E0BC7E35}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{121675BF-0F4A-41A6-8B66-C931E0BC7E35}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{121675BF-0F4A-41A6-8B66-C931E0BC7E35}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {0243AB2A-6CBE-4BBF-A9CB-F782BB03E418}
24+
EndGlobalSection
25+
EndGlobal

0 commit comments

Comments
 (0)