Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. Imports Microsoft.AnalysisServices.AdomdClient
  2. Public Class Form1
  3. Function connect2measures() As Array
  4. Dim conn As New AdomdConnection("Provider=SQLNCLI11.1;Data Source=MV-W81-32BITS;" & _
  5. "Integrated Security=SSPI;Initial Catalog=L1")
  6. Dim m1 As Array
  7. conn.Open()
  8. Dim cmd As New AdomdCommand("SELECT {[Measures].AllMembers} ON 0" & _
  9. " FROM [Adventure Works DW2012-cube]")
  10. cmd.Connection = conn
  11. m1 = connect(cmd, 1)
  12. conn.Close()
  13. Return m1
  14. End Function
  15. Function connect2data(measure As String) As Array
  16. Dim conn As New AdomdConnection("Provider=SQLNCLI11.1;Data Source=MV-W81-32BITS;" & _
  17. "Integrated Security=SSPI;Initial Catalog=L1")
  18. Dim m2 As Array
  19. conn.Open()
  20. Dim cmd As New AdomdCommand("SELECT NON EMPTY [Order Date].[All].Children ON rows," & _
  21. " NON EMPTY [Dim Product].[All].children ON columns FROM [Adventure Works DW2012-cube]" & _
  22. " WHERE " & measure)
  23. cmd.Connection = conn
  24. m2 = connect(cmd, 2)
  25. conn.Close()
  26. Return m2
  27. End Function
  28. Function connect(cmd As AdomdCommand, AxesNo As Integer) As Array
  29. Dim noLines As Integer
  30. Dim noCols As Integer
  31. Dim matrix(0, 0) As String
  32. Dim cs As CellSet
  33. cs = cmd.ExecuteCellSet
  34. noCols = cs.Axes(0).Positions.Count
  35. If AxesNo = 1 Then
  36. noLines = 1
  37. Else
  38. noLines = cs.Axes(1).Positions.Count
  39. End If
  40. Dim axis As Axis
  41. If AxesNo > 1 Then
  42. ReDim matrix(noCols, noLines)
  43. matrix(0, 0) = ""
  44. For i = 0 To noCols - 1
  45. matrix(0, i + 1) = cs.Axes(0).Positions(i).Members(0).Caption
  46. file:///C|/Users/LIVES4BIT/Desktop/L3-measure%20filter%20(form%20source%20code).txt (1 of 3)3/22/2017 5:54:20 PM
  47. file:///C|/Users/LIVES4BIT/Desktop/L3-measure%20filter%20(form%20source%20code).txt
  48. axis = cs.Axes(1)
  49. For j = 0 To noLines - 1
  50. matrix(j + 1, 0) = axis.Positions(j).Members(0).Caption
  51. matrix(j + 1, i + 1) = Math.Round(cs(i, j).Value, 2, MidpointRounding.AwayFromZero)
  52. Next
  53. Next
  54. Else
  55. ReDim matrix(noLines - 1, noCols)
  56. matrix(0, 0) = ""
  57. For i = 0 To noCols - 1
  58. matrix(0, i + 1) = cs.Axes(0).Positions(i).Members(0).Caption
  59. Next
  60. End If
  61. Return matrix
  62. End Function
  63. Function CreateDataView(matrix As Array) As ICollection
  64. Dim noLines As Integer
  65. Dim noCols As Integer
  66. noCols = matrix.GetLength(1) - 1
  67. noLines = matrix.GetLength(0) - 1
  68. Dim dt As New DataTable()
  69. Dim dr As DataRow
  70. If matrix.Length > 1 Then
  71. dt.Columns.Add(New DataColumn("Year", GetType(String)))
  72. Dim k, l As Integer
  73. For k = 0 To noCols - 1
  74. dt.Columns.Add(New DataColumn(matrix(0, k + 1), GetType(String)))
  75. Next
  76. For l = 1 To noLines
  77. dr = dt.NewRow()
  78. For k = 0 To noCols
  79. dr(k) = matrix(l, k)
  80. Next
  81. dt.Rows.Add(dr)
  82. Next
  83. End If
  84. Dim dv As New DataView(dt)
  85. Return dv
  86. End Function
  87. Private Sub buildChart(matrix As Array)
  88. Chart1.Series.Clear()
  89. Dim noLines As Integer
  90. Dim noCols As Integer
  91. noCols = matrix.GetLength(1) - 1
  92. file:///C|/Users/LIVES4BIT/Desktop/L3-measure%20filter%20(form%20source%20code).txt (2 of 3)3/22/2017 5:54:20 PM
  93. file:///C|/Users/LIVES4BIT/Desktop/L3-measure%20filter%20(form%20source%20code).txt
  94. noLines = matrix.GetLength(0) - 1
  95. For k = 1 To noCols
  96. Chart1.Series.Add(matrix(0, k))
  97. Next
  98. For l = 1 To noLines
  99. For k = 1 To noCols
  100. Chart1.Series(matrix(0, k)).Points.AddXY(matrix(l, 0), matrix(l, k))
  101. Next
  102. Next
  103. End Sub
  104. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  105. DataGridView1.DataSource = Nothing
  106. Dim measure As String = "[Measures].[" & ComboBox1.SelectedItem.ToString() & "]"
  107. DataGridView1.DataSource = CreateDataView(connect2data(measure))
  108. End Sub
  109. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  110. Dim measure As String = "[Measures].[" & ComboBox1.SelectedItem.ToString() & "]"
  111. buildChart(connect2data(measure))
  112. End Sub
  113. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  114. Dim m1, m2 As Array
  115. m1 = connect2measures()
  116. For i = 1 To m1.GetLength(1) - 1
  117. ComboBox1.Items.Add(m1(0, i))
  118. Next
  119. ComboBox1.SelectedIndex = 3
  120. Dim measure As String = "[Measures].[Sales Amount]"
  121. m2 = connect2data(measure)
  122. DataGridView1.DataSource = CreateDataView(m2)
  123. buildChart(m2)
  124. End Sub
  125. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement