-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectionStringsStorage.cs
85 lines (74 loc) · 4.02 KB
/
ConnectionStringsStorage.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
using DevExpress.DashboardCommon;
using DevExpress.DashboardWeb;
using DevExpress.DataAccess.ConnectionParameters;
using DevExpress.DataAccess.Json;
using DevExpress.DataAccess.Web;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Entity;
using System.Linq;
namespace DXWebApplication26 {
public class ConnectionStringsStorage : IDataSourceWizardConnectionStringsStorage {
static Dictionary<string, DataConnectionParametersBase> storage;
string connectionString;
static Dictionary<string, DataConnectionParametersBase> Storage {
get {
if (storage == null)
storage = new Dictionary<string, DataConnectionParametersBase>();
return storage;
}
}
public ConnectionStringsStorage(string connectionString) {
storage = CreateNewStorage();
this.connectionString = connectionString;
}
Dictionary<string, DataConnectionParametersBase> CreateNewStorage() {
var dictionary = new Dictionary<string, DataConnectionParametersBase>();
// Load existing SQL Connection Strings from Web.config
foreach (ConnectionStringSettings connectionStringSettings in ConfigurationManager.ConnectionStrings)
dictionary.Add(connectionStringSettings.Name, new CustomStringConnectionParameters(connectionStringSettings.ConnectionString));
// Add predefined JSON connections
var jsonConnectionParams = new JsonSourceConnectionParameters();
jsonConnectionParams.JsonSource = new UriJsonSource(new Uri("https://raw.githubusercontent.com/DevExpress-Examples/DataSources/master/JSON/customers.json"));
dictionary.Add("Customers_JSON", jsonConnectionParams);
// Load JSON connections from a DB
using (JsonConnectionContext context = new JsonConnectionContext()) {
context.Connections.ToList().ForEach(c => {
if (!dictionary.ContainsKey(c.Name))
dictionary.Add(c.Name, new CustomStringConnectionParameters(c.ConnectionString));
else
dictionary[c.Name] = new CustomStringConnectionParameters(c.ConnectionString);
});
}
return dictionary;
}
Dictionary<string, string> IDataSourceWizardConnectionStringsProvider.GetConnectionDescriptions() {
return Storage.Keys.ToDictionary(key => key, value => value);
}
DataConnectionParametersBase IDataSourceWizardConnectionStringsProvider.GetDataConnectionParameters(string name) {
return Storage[name];
}
// Save JSON connections to a DB
void IDataSourceWizardConnectionStringsStorage.SaveDataConnectionParameters(string name, DataConnectionParametersBase connectionParameters, bool saveCredentials) {
JsonSourceConnectionParameters jsonParams = connectionParameters as JsonSourceConnectionParameters;
if (jsonParams != null) {
using (JsonConnectionContext context = new JsonConnectionContext()) {
JsonDataConnection dataConnection = new JsonDataConnection(jsonParams.JsonSource);
var existingConnection = context.Connections.Where(c => c.Name == name).FirstOrDefault();
if (existingConnection == null) {
MyJsonConnection jsonConnection = new MyJsonConnection();
jsonConnection.Name = name;
jsonConnection.ConnectionString = dataConnection.CreateConnectionString();
context.Connections.Add(jsonConnection);
} else {
existingConnection.ConnectionString = dataConnection.CreateConnectionString();
}
context.SaveChanges();
}
Storage[name] = new CustomStringConnectionParameters(connectionString);
}
}
}
}