-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathLocalDBManager.cs
More file actions
79 lines (70 loc) · 2.47 KB
/
Copy pathLocalDBManager.cs
File metadata and controls
79 lines (70 loc) · 2.47 KB
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
using System.Diagnostics;
using System.Net;
using System.Security.Principal;
using Microsoft.Data.SqlClient;
using WorkloadTools;
namespace ConvertWorkload
{
internal class LocalDBManager
{
public bool IsElevated
{
get
{
return new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);
}
}
public string DownloadLocalDB()
{
var localPath = Path.GetTempPath() + "SqlLocalDB.msi";
using (var client = new WebClient())
{
var wp = WebRequest.DefaultWebProxy;
wp.Credentials = CredentialCache.DefaultCredentials;
client.Proxy = wp;
client.DownloadFile("https://download.microsoft.com/download/7/c/1/7c14e92e-bdcb-4f89-b7cf-93543e7112d1/SqlLocalDB.msi", localPath);
}
return localPath;
}
public void InstallLocalDB()
{
if (!IsElevated)
{
throw new InvalidOperationException("Installing LocalDB requires elevation.");
}
var localFileName = DownloadLocalDB();
var p = new Process();
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.FileName = "c:\\windows\\system32\\msiexec.exe";
p.StartInfo.Arguments = " /i "+ localFileName +" /qn IACCEPTSQLLOCALDBLICENSETERMS=YES";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
//Vista or higher check
if (System.Environment.OSVersion.Version.Major >= 6)
{
p.StartInfo.Verb = "runas";
}
p.Start();
p.WaitForExit();
}
public bool CanConnectToLocalDB()
{
try
{
var info = new SqlConnectionInfo();
info.ServerName = @"(localdb)\MSSQLLocalDB";
info.UseIntegratedSecurity = true;
using (var conn = new SqlConnection(info.ConnectionString() + ";Connect Timeout=30;"))
{
conn.Open();
}
return true;
}
catch(Exception)
{
return false;
}
}
}
}