-
Notifications
You must be signed in to change notification settings - Fork 1
/
DatabaseEditableDashboardStorage.vb
94 lines (79 loc) · 3.38 KB
/
DatabaseEditableDashboardStorage.vb
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
89
90
91
92
93
94
Imports Microsoft.VisualBasic
Imports DevExpress.DashboardWeb
Imports System
Imports System.Collections.Generic
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Linq
Imports System.Text
Imports System.Xml.Linq
Namespace T386418
Public Class DatabaseEditableDashboardStorage
Implements IEditableDashboardStorage
Private connectionString As String
Public Sub New(ByVal connectionString As String)
MyBase.New()
Me.connectionString = connectionString
End Sub
Private Function AddDashboard(ByVal document As XDocument, ByVal dashboardName As String) As String Implements IEditableDashboardStorage.AddDashboard
Using connection As New SqlConnection(connectionString)
connection.Open()
Dim stream As New MemoryStream()
document.Save(stream)
stream.Position = 0
Dim InsertCommand As 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
Dim ID As String = InsertCommand.ExecuteScalar().ToString()
connection.Close()
Return ID
End Using
End Function
Private Function LoadDashboard(ByVal dashboardID As String) As XDocument Implements IDashboardStorage.LoadDashboard
Using connection As New SqlConnection(connectionString)
connection.Open()
Dim GetCommand As New SqlCommand("SELECT Dashboard FROM Dashboards WHERE ID=@ID")
GetCommand.Parameters.Add("ID", SqlDbType.Int).Value = Convert.ToInt32(dashboardID)
GetCommand.Connection = connection
Dim reader As SqlDataReader = GetCommand.ExecuteReader()
reader.Read()
Dim data() As Byte = TryCast(reader.GetValue(0), Byte())
Dim stream As New MemoryStream(data)
connection.Close()
Return XDocument.Load(stream)
End Using
End Function
Private Function GetAvailableDashboardsInfo() As IEnumerable(Of DashboardInfo) Implements IDashboardStorage.GetAvailableDashboardsInfo
Dim list As New List(Of DashboardInfo)()
Using connection As New SqlConnection(connectionString)
connection.Open()
Dim GetCommand As New SqlCommand("SELECT ID, Caption FROM Dashboards")
GetCommand.Connection = connection
Dim reader As SqlDataReader = GetCommand.ExecuteReader()
Do While reader.Read()
Dim ID As String = reader.GetInt32(0).ToString()
Dim Caption As String = reader.GetString(1)
list.Add(New DashboardInfo() With {.ID = ID, .Name = Caption})
Loop
connection.Close()
End Using
Return list
End Function
Private Sub SaveDashboard(ByVal dashboardID As String, ByVal document As XDocument) Implements IDashboardStorage.SaveDashboard
Using connection As New SqlConnection(connectionString)
connection.Open()
Dim stream As New MemoryStream()
document.Save(stream)
stream.Position = 0
Dim InsertCommand As 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()
End Using
End Sub
End Class
End Namespace