-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
71 lines (60 loc) · 2.65 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
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;
using Azure.Core;
using System;
using System.Text;
using Microsoft.Data.SqlClient;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
SecretClientOptions options = new SecretClientOptions()
{
Retry =
{
Delay= TimeSpan.FromSeconds(2),
MaxDelay = TimeSpan.FromSeconds(16),
MaxRetries = 5,
Mode = RetryMode.Exponential
}
};
var client = new SecretClient(new Uri("<your-kv-uri>"), new DefaultAzureCredential(),options);
// or if accessing the keyvault reference use the Configuration.Get () method
KeyVaultSecret secret = client.GetSecret("<name-of-secret>");
string str="";
string secretValue = secret.Value;
try
{
SqlConnectionStringBuilder builder2 = new SqlConnectionStringBuilder();
builder2.DataSource = "<name-of-your-sql-server>.database.windows.net";
builder2.UserID = "<your-sql-username>";
builder2.Password = secretValue;
builder2.InitialCatalog = "<your-sql-db-name>";
using (SqlConnection connection = new SqlConnection(builder2.ConnectionString))
{
Console.WriteLine("\nQuery data example:");
Console.WriteLine("=========================================\n");
String sql = "SELECT * FROM inventory";
using (SqlCommand command = new SqlCommand(sql, connection))
{
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{ var i = 0;
while (reader.Read() )
{
Console.WriteLine(string.Format(
"Reading from table=({0},{1})",
reader.GetString(0),reader.GetInt32(1)));
str=str+"\n"+string.Format(
"Reading from table=({0},{1})",
reader.GetString(0),reader.GetInt32(1));
}
}
connection.Close();
}
}
}
catch (SqlException e)
{
Console.WriteLine(e.ToString());
}
app.MapGet("/", () => str);
app.Run();