This repository has been archived by the owner on Jan 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
SQLServerUtils.cs
51 lines (45 loc) · 1.64 KB
/
SQLServerUtils.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
using System.Data.SqlClient;
using Microsoft.AspNetCore.Http;
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace MindFlavor.SQLServerExporter
{
public struct SQLServerInfo
{
public string ConnectionString;
public string Name;
public string Version;
}
public class SQLServerUtils
{
public static SQLServerInfo GetSQLServerInfo(HttpContext context, string connectionString)
{
var logger = context.RequestServices.GetRequiredService<ILogger<SQLServerUtils>>();
using (SqlConnection conn = new SqlConnection(connectionString))
{
logger.LogTrace($"Opening connection to {conn.ConnectionString}");
conn.Open();
using (SqlCommand cmd = new SqlCommand(TSQLStore.Entries["name_and_version"]["generic"], conn))
{
logger.LogDebug($"Performing {cmd.CommandText}");
using (var reader = cmd.ExecuteReader())
{
reader.Read();
return new SQLServerInfo { ConnectionString = connectionString, Name = reader.GetString(0), Version = reader.GetString(1) };
}
}
}
}
}
}