-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataBaseEditaleDashboardStorage.cs
88 lines (79 loc) · 3.89 KB
/
DataBaseEditaleDashboardStorage.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
86
87
88
using DevExpress.DashboardWeb;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Xml.Linq;
namespace SaveDashboardDB {
public class DataBaseEditaleDashboardStorage : IEditableDashboardStorage {
private string connectionString;
public DataBaseEditaleDashboardStorage(string connectionString) {
this.connectionString = connectionString;
}
public string AddDashboard(XDocument document, string dashboardName) {
using (SqlConnection connection = new SqlConnection(connectionString)) {
connection.Open();
MemoryStream stream = new MemoryStream();
document.Save(stream);
stream.Position = 0;
SqlCommand InsertCommand = new SqlCommand(
"INSERT INTO Dashboards (Dashboard, Caption) " +
"output INSERTED.ID " +
"VALUES (@Dashboard, @Caption)");
InsertCommand.Parameters.Add("Caption", SqlDbType.NVarChar).Value = dashboardName;
InsertCommand.Parameters.Add("Dashboard", SqlDbType.VarBinary).Value = stream.ToArray();
InsertCommand.Connection = connection;
string ID = InsertCommand.ExecuteScalar().ToString();
connection.Close();
return ID;
}
}
public XDocument LoadDashboard(string dashboardID) {
using (SqlConnection connection = new SqlConnection(connectionString)) {
connection.Open();
SqlCommand GetCommand = new SqlCommand("SELECT Dashboard FROM Dashboards WHERE ID=@ID");
GetCommand.Parameters.Add("ID", SqlDbType.Int).Value = Convert.ToInt32(dashboardID);
GetCommand.Connection = connection;
SqlDataReader reader = GetCommand.ExecuteReader();
reader.Read();
byte[] data = reader.GetValue(0) as byte[];
MemoryStream stream = new MemoryStream(data);
connection.Close();
return XDocument.Load(stream);
}
}
public IEnumerable<DashboardInfo> GetAvailableDashboardsInfo() {
List<DashboardInfo> list = new List<DashboardInfo>();
using (SqlConnection connection = new SqlConnection(connectionString)) {
connection.Open();
SqlCommand GetCommand = new SqlCommand("SELECT ID, Caption FROM Dashboards");
GetCommand.Connection = connection;
SqlDataReader reader = GetCommand.ExecuteReader();
while (reader.Read()) {
string ID = reader.GetInt32(0).ToString();
string Caption = reader.GetString(1);
list.Add(new DashboardInfo() { ID = ID, Name = Caption });
}
connection.Close();
}
return list;
}
public void SaveDashboard(string dashboardID, XDocument document) {
using (SqlConnection connection = new SqlConnection(connectionString)) {
connection.Open();
MemoryStream stream = new MemoryStream();
document.Save(stream);
stream.Position = 0;
SqlCommand InsertCommand = new SqlCommand(
"UPDATE Dashboards Set Dashboard = @Dashboard " +
"WHERE ID = @ID");
InsertCommand.Parameters.Add("ID", SqlDbType.Int).Value = Convert.ToInt32(dashboardID);
InsertCommand.Parameters.Add("Dashboard", SqlDbType.VarBinary).Value = stream.ToArray();
InsertCommand.Connection = connection;
InsertCommand.ExecuteNonQuery();
connection.Close();
}
}
}
}