-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
153 lines (130 loc) · 5.29 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using System.Data.SqlClient;
using System.Diagnostics;
using TestApplication.Shared;
namespace TestApplication.SqlClient.System;
#pragma warning disable CS0618 // Type or member is obsolete, System.Data.SqlClient classes are deprecated in 4.9.0+
/// <summary>
/// This test application uses SqlConnection from System.Data.SqlClient (NuGet package).
/// </summary>
public class Program
{
private const string CreateCommand = "CREATE TABLE MY_TABLE ( Id int, Value1 varchar(255), Value2 varchar(255) )";
private const string DropCommand = "DROP TABLE MY_TABLE";
private const string InsertCommand = "INSERT INTO MY_TABLE VALUES ( 1, 'value1', 'value2' )";
private const string SelectCommand = "SELECT * FROM MY_TABLE";
public static async Task Main(string[] args)
{
ConsoleHelper.WriteSplashScreen(args);
(string databasePassword, string databasePort) = ParseArgs(args);
var connectionString = GetConnectionString(databasePassword, databasePort);
using (var connection = new SqlConnection(connectionString))
{
ExecuteCommands(connection);
}
using (var connection = new SqlConnection(connectionString))
{
await ExecuteAsyncCommands(connection);
}
// The "LONG_RUNNING" environment variable is used by tests that access/receive
// data that takes time to be produced.
var longRunning = Environment.GetEnvironmentVariable("LONG_RUNNING");
if (longRunning == "true")
{
// In this case it is necessary to ensure that the test has a chance to read the
// expected data, only by keeping the application alive for some time that can
// be ensured. Anyway, tests that set "LONG_RUNNING" env var to true are expected
// to kill the process directly.
Console.WriteLine("LONG_RUNNING is true, waiting for process to be killed...");
Process.GetCurrentProcess().WaitForExit();
}
}
private static void ExecuteCommands(SqlConnection connection)
{
connection.Open();
ExecuteCreate(connection);
ExecuteInsert(connection);
ExecuteSelect(connection);
ExecuteDrop(connection);
}
private static void ExecuteCreate(SqlConnection connection)
{
ExecuteCommand(CreateCommand, connection);
}
private static void ExecuteInsert(SqlConnection connection)
{
ExecuteCommand(InsertCommand, connection);
}
private static void ExecuteSelect(SqlConnection connection)
{
ExecuteCommand(SelectCommand, connection);
}
private static void ExecuteDrop(SqlConnection connection)
{
ExecuteCommand(DropCommand, connection);
}
private static void ExecuteCommand(string commandString, SqlConnection connection)
{
try
{
using var command = new SqlCommand(commandString, connection);
using var reader = command.ExecuteReader();
Console.WriteLine($"SQL query executed successfully: {commandString}");
}
catch (Exception ex)
{
Console.WriteLine($"Error while executing SQL query: {commandString}.\n{ex.Message}");
}
}
private static async Task ExecuteAsyncCommands(SqlConnection connection)
{
await connection.OpenAsync();
await ExecuteCreateAsync(connection);
await ExecuteInsertAsync(connection);
await ExecuteSelectAsync(connection);
await ExecuteDropAsync(connection);
}
private static async Task ExecuteCommandAsync(string commandString, SqlConnection connection)
{
try
{
using var command = new SqlCommand(commandString, connection);
using var reader = await command.ExecuteReaderAsync();
Console.WriteLine($"Async SQL query executed successfully: {commandString}");
}
catch (Exception ex)
{
Console.WriteLine($"Error while executing async SQL query: {commandString}.\n{ex.Message}");
}
}
private static async Task ExecuteCreateAsync(SqlConnection connection)
{
await ExecuteCommandAsync(CreateCommand, connection);
}
private static async Task ExecuteInsertAsync(SqlConnection connection)
{
await ExecuteCommandAsync(InsertCommand, connection);
}
private static async Task ExecuteSelectAsync(SqlConnection connection)
{
await ExecuteCommandAsync(SelectCommand, connection);
}
private static async Task ExecuteDropAsync(SqlConnection connection)
{
await ExecuteCommandAsync(DropCommand, connection);
}
private static string GetConnectionString(string databasePassword, string databasePort)
{
return $"Server=127.0.0.1,{databasePort};User=sa;Password={databasePassword};TrustServerCertificate=True;";
}
private static (string DatabasePassword, string Port) ParseArgs(IReadOnlyList<string> args)
{
if (args?.Count != 2)
{
throw new ArgumentException($"{nameof(TestApplication.SqlClient)}: requires two command-line arguments: <dbPassword> <dbPort>");
}
return (DatabasePassword: args[0], Port: args[1]);
}
}
#pragma warning restore CS0618 // Type or member is obsolete