-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
81 lines (70 loc) · 2.24 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
using CommandLine;
using Microsoft.Data.SqlClient;
var endpoint = "";
var user = "admin";
var password = "";
var label = "";
Parser.Default.ParseArguments<CommandLineOptions>(args)
.WithParsed<CommandLineOptions>(o =>
{
endpoint = o.Endpoint;
label = o.Label;
user = o.User;
password = o.Password;
})
.WithNotParsed<CommandLineOptions>(o =>
{
System.Environment.Exit(0);
});
var builder = new SqlConnectionStringBuilder()
{
DataSource = endpoint,
UserID = user,
Password = password,
TrustServerCertificate = true
};
int RetryMaxAttempts = 60;
int RetryIntervalPeriodInSeconds = 1;
var sql = "SELECT name, collation_name FROM sys.databases";
int iRetryCount = 0;
while (iRetryCount < RetryMaxAttempts)
{
try
{
Log($"Opening a DB connection to {builder.DataSource}");
using var connection = new SqlConnection(builder.ConnectionString);
connection.Open();
using var command = new SqlCommand(sql, connection);
while (true)
{
Log("Starting a query...");
using var reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine("{0} {1}", reader.GetString(0), reader.GetString(1));
}
Thread.Sleep(1000);
}
}
catch (Exception ex)
{
iRetryCount++;
Log($"ERROR: {ex.Message}, retry #{iRetryCount}");
}
Thread.Sleep(RetryIntervalPeriodInSeconds * 1000);
}
void Log(string msg)
{
Console.WriteLine($"{DateTime.Now} : {label} {msg}");
}
public class CommandLineOptions
{
[Option('e', "endpoint", Required = true, HelpText = "MS SQL endpoint")]
public string? Endpoint { get; set; }
[Option('u', "user", Required = true, Default = "admin", HelpText = "DB username")]
public string? User { get; set; }
[Option('p', "password", Required = true, HelpText = "DB password")]
public string? Password { get; set; }
[Option('l', "label", Required = false, Default = "", HelpText = "Free-text label which will be used as prefix in stdout messages")]
public string? Label { get; set; }
}