Microsoft SQL Server 2005 Developer’s Guide- P46 pot

10 143 0
Microsoft SQL Server 2005 Developer’s Guide- P46 pot

Đang tải... (xem toàn văn)

Thông tin tài liệu

Chapter 11: Developing BI Applications with ADOMD.NET 429 lvItem.SubItems.Add(String.Empty) End If Next iField ' Add the item to the listview rstListView.Items.Add(lvItem) Loop While (dr.Read()) ' Close the DataReader dr.Close() Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub Private Function ParseColName(ByRef sColName As String) As String Dim sShortName As String Dim iFound As Integer = sColName.LastIndexOf("[") + 1 sShortName = sColName.Substring(iFound, sColName.Length() - _ (iFound + 1)) Return sShortName End Function At the top of the subroutine, you can see that an AdomdConnection object is passed in. The next statement creates a new AdomdCommand object named cmd and uses an MDX SELECT statement as a parameter on the constructor. This very simple MDX SELECT statement, when executed, will set the column and row dimensions of the multidimensional results with employee last names and English name months. The NON EMPTY keywords are used, so that only the nonempty data will be selected. Next, an AdomdDataReader is initialized to receive the multidimensional data, and a ListViewItem object is initialized. The next two lines clear the Items and Columns from the ListView control that was placed on the Windows form at the program design phase. The AdomdCommand’s ExecuteReader method is then called and returns the AdomdDataReader named dr. As you can see from the code listing, the ExecuteReader is called inside the Try-Catch loop. Any exceptions that may occur are trapped by the Try-Catch loop, and a message will be displayed to the user. After the data has been retrieved with the ExecuteReader method and output to the dr AdomdDataReader object, the Read method of the dr object is called to read the first data and set up the column names for output to the ListView control. The next few lines of code use a For Next loop to add columns to the ListView control. In this example, the dr object’s GetName method is used to set the column text with the names of the 430 Microsoft SQL Server 2005 Developer’s Guide retrieved columns. Because the GetName method returns the complete name of the column, including the dimension hierarchy, we use a simple ParseColName function to strip the column name of unwanted characters. For example, the dr.GetName method returns the value of ‘[Dim Time].[English Month Name].&[April]’. We want only the month name to appear on the column heading of the ListView control, so we pass the whole string to the ParseColName function and strip out the unwanted hierarchy description, returning the short text value of ‘April’. The ParseColName function is included in the code listing. Once each of the columns has been added to the ListView control and the column names have been added to the column text, a Do While is used to initialize a new ListViewItem and read each element of the AdomdDataReader. Inside the Do While loop, a For Next loop iterates through each of the dr object’s row items and adds them to the new ListViewItem. The Do While loop then reads the next row item of the dr object using the dr.Read method, and adds a ListViewItem to the ListView control until all of the rows of the dr object have been read. The dr AdomdDataReader object is then closed using the dr.Close method, and the results are displayed to the user, as shown in Figure 11-4. Figure 11-4 AdomdDataReader results Chapter 11: Developing BI Applications with ADOMD.NET 431 Using the XMLReader Object The AdomdCommand object also allows returned multidimensional data to be displayed in XML format. To view data in XML format, the AdomdCommand’s ExecuteXmlReader method is used that returns an XmlReader object. The next subroutine shows retrieving data to an XmlReader and displaying it in a Listbox control: Private Sub XMLReader(ByRef cn As AdomdConnection) Dim cmd As New AdomdCommand("SELECT NON EMPTY " & _ "[Dim Time].[English Month Name].MEMBERS ON COLUMNS, " & _ "NON EMPTY {[Dim Employee].[Last Name].MEMBERS} ON ROWS " & _ "FROM [AdventureWorksDW]", cn) Dim xmlReader As System.Xml.XmlReader Try ' Execute the XML query xmlReader = cmd.ExecuteXmlReader() xmlReader.MoveToContent() While xmlReader.Read() Select Case xmlReader.NodeType Case XmlNodeType.Element rstListBox.Items.Add("<{0}>" & xmlReader.Name) Case XmlNodeType.Text rstListBox.Items.Add(xmlReader.Value) Case XmlNodeType.CDATA rstListBox.Items.Add("<![CDATA[{0}]]>" & _ xmlReader.Value) Case XmlNodeType.ProcessingInstruction rstListBox.Items.Add("<?{0} {1}?>" & _ xmlReader.Name & xmlReader.Value) Case XmlNodeType.Comment rstListBox.Items.Add("<! {0} >" & _ xmlReader.Value) Case XmlNodeType.XmlDeclaration rstListBox.Items.Add("<?xml version='1.0'?>") Case XmlNodeType.Document Case XmlNodeType.DocumentType rstListBox.Items.Add("<!DOCTYPE {0} [{1}]" & _ xmlReader.Name & xmlReader.Value) Case XmlNodeType.EntityReference rstListBox.Items.Add(xmlReader.Name) 432 Microsoft SQL Server 2005 Developer’s Guide Case XmlNodeType.EndElement rstListBox.Items.Add("</{0}>" & xmlReader.Name) End Select End While xmlReader.Close() Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub As you can see at the top of the listing, an AdomdConnection object is passed in to the subroutine. The next statement creates the AdomdCommand object using the MDX SELECT statement in its constructor. The next line shows the creation of an XmlReader object. The XmlReader is found in the System.Xml namespace; therefore, this line of code shows the creation of the XmlReader using the fully qualified namespace hierarchy. The AdomdCommand’s ExecuteXmlReader is then executed and returns the xmlReader object. The Try-Catch loop is employed here to catch any exceptions that may occur and displays the exception message to the user. The next statement calls the xmlReader’s MoveToContent method that skips over random XML markup. The xmlReader’s Read method is called in a While loop to read through each retrieved row in the xmlReader. Inside the While loop a Select Case statement is used to format the information and add it to the Listbox control found on the Windows form. When all of the rows have been read from the xmlReader, it is closed and the resulting Listbox is displayed to the user. Using the CellSet Object The ExecuteCellSet method of the AdomdCommand object is called to return multidimensional results to a CellSet. A CellSet is similar to a DataSet; however, a DataSet can contain only two-dimensional relational data, but a CellSet can contain multidimensional data. A CellSet’s contents consist of a collection of cells that are organized along multiple dimensions. The code listing that follows shows creating a CellSet with an AdomdCommand’s ExecuteCellSet method and displays the retrieved information in a Listbox: Private Sub CellSet(ByRef cn As AdomdConnection) Dim cmd As New AdomdCommand("SELECT NON EMPTY " & _ "[Dim Time].[English Month Name].MEMBERS ON COLUMNS, " & _ "NON EMPTY {[Dim Employee].[Last Name].MEMBERS} ON ROWS " & _ Chapter 11: Developing BI Applications with ADOMD.NET 433 "FROM [AdventureWorksDW]", cn) Dim cs As CellSet ' Clear the ListBox rstListBox.Items.Clear() Try ' Execute the query and return a cellset cs = cmd.ExecuteCellSet() rstListBox.Items.Add("The cellset has " & cs.Cells.Count & _ " cells organized along " & cs.Axes.Count & " axes") Dim axCol As Axis = cs.Axes(0) Dim axRow As Axis = cs.Axes(1) Dim posRow As Position, posCol As Position For Each posRow In axRow.Positions Dim sCell As String = 0 For Each posCol In axCol.Positions sCell += cs(posCol.Ordinal, _ posRow.Ordinal).FormattedValue() & vbTab Next ' Add the item to the listbox rstListBox.Items.Add(sCell) Next Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub Again, an AdomdConnection object is passed in at the top of the subroutine and an AdomdCommand object is created using the SELECT MDX statement. A CellSet object is then created, and the Windows form Listbox control is cleared of any leftover information. The next statement shows calling the AdomdCommand’s ExecuteCellSet method and returning the results to the CellSet. The CellSet now contains a collection of query axes, which help to organize the information within the CellSet, and a collection of cells. In this example, there are two axes in the CellSet Axes collection, one for the columns in the CellSet and one for the rows in the CellSet. The next statements initialize variables for the Axes collections and Position variables to select and output the coordinates of the 434 Microsoft SQL Server 2005 Developer’s Guide cells in the CellSet. Nested For Next loops are set up next, to iterate through the axes and add the cells to the Listbox according to the position coordinates of the cells. The final results are displayed to the user as shown in Figure 11-5. Using the AdomdDataAdapter Object The AdomdDataAdapter is used in combination with the AdomdConnection object and the AdomdCommand object to fill a CellSet with multidimensional data and then resolve the information back to a SQL Server database. The following example illustrates how to use an AdomdConnection, create an AdomdCommand object, and populate a new DataTable with the AdomdDataAdapter. The contents of the DataTable will then be displayed to the user in a grid: Private Sub AdomdDataAdapter(ByRef cn As AdomdConnection) Dim cmd As New AdomdCommand("SELECT NON EMPTY " & _ "[Dim Time].[English Month Name].MEMBERS ON COLUMNS, " & _ "NON EMPTY {[Dim Employee].[Last Name].MEMBERS} ON ROWS " & _ "FROM [AdventureWorksDW]", cn) Dim da As New AdomdDataAdapter(cmd) Dim dt As New DataTable Figure 11-5 CellSet results Chapter 11: Developing BI Applications with ADOMD.NET 435 Try da.Fill(dt) rstDataGridView.DataSource = dt Catch ex As Exception MessageBox.Show(ex.Message) End Try En d Sub An instance of the AdomdConnection object is passed in at the top of the subroutine. The next statement creates an AdomdCommand object and sets its CommandText property to the SELECT MDX statement and Connection property to the previously passed-in AdomdConnection object. Next, an instance of a AdomdDataAdapter is created and its SelectCommand property is set to the AdomdCommand object. An empty DataTable is then created, which will be populated with the results of the SELECT query command. The DataTable is then filled using the AdomdDataAdapter’s Fill method, which is executed inside a Try-Catch block. If the Fill method fails, the code in the Catch block is executed and a message box appears showing the error message. Finally, a DataGrid’s DataSource property is set to the DataTable and displayed to the user as shown in Figure 11-6. Figure 11-6 AdomdDataAdapter results 436 Microsoft SQL Server 2005 Developer’s Guide Using the CubeDef Object Using the CubeDef object in ADOMD.NET, you can retrieve metadata information about a cube, including its dimensions, measures, and named sets. The CubeDef object contains only metadata information and no actual cell data. The AdomdConnection object contains a collection of cubes that are in the database specified for the AdomdConnection object. The following code listing shows how to display some metadata information from a cube in a Listbox control: Private Sub CubeDef(ByRef cn As AdomdConnection) Dim cubDef As CubeDef = cn.Cubes(0) ' Clear the Listbox rstListBox.Items.Clear() rstListBox.Items.Add(" ** Measures ** ") For Each meas As Measure In cubDef.Measures rstListBox.Items.Add("Name : " & meas.Name) rstListBox.Items.Add("Description : " & meas.Description) rstListBox.Items.Add("Expression : " & meas.Expression) rstListBox.Items.Add("Units : " & meas.Units) Next rstListBox.Items.Add(" ** Dimensions ** ") For Each dimen As Dimension In cubDef.Dimensions rstListBox.Items.Add("Name : " & dimen.Name) rstListBox.Items.Add("Description : " & dimen.Description) rstListBox.Items.Add("Hierarchy : " & _ dimen.Hierarchies(0).ToString()) Next End Sub The AdomdConnection object is passed in at the top of the subroutine, and a CubeDef object is created and set with the information from the first cube in the AdomdConnection’s Cubes collection. The CubeDef contains collections for Dimensions, Measures, NamedSets, and KPIs that are associated with each specified cube in the database. The next statements in the code listing clear the Listbox of any previous items, and then two For Next loops are set up to iterate through the Measures collection of the CubeDef and the Dimensions collection of the CubeDef, adding each of the collection elements to the Listbox for display to the user. Chapter 11: Developing BI Applications with ADOMD.NET 437 Summary ADOMD.NET is a database provider that allows you to develop database applications that communicate with multidimensional data sources. In this chapter you learned about some of the SQL Server Analysis Services capabilities as well as how to develop BI applications that access some of those capabilities. SSAS allows you to analyze your data to determine trends and patterns to meet your business goals. Developing visual applications with ADOMD.NET to show those trend and patterns can increase the usability of that information. This page intentionally left blank . object’s GetName method is used to set the column text with the names of the 430 Microsoft SQL Server 2005 Developer’s Guide retrieved columns. Because the GetName method returns the complete. xmlReader.Value) Case XmlNodeType.EntityReference rstListBox.Items.Add(xmlReader.Name) 432 Microsoft SQL Server 2005 Developer’s Guide Case XmlNodeType.EndElement rstListBox.Items.Add("</{0}>". collections and Position variables to select and output the coordinates of the 434 Microsoft SQL Server 2005 Developer’s Guide cells in the CellSet. Nested For Next loops are set up next, to iterate

Ngày đăng: 05/07/2014, 05:20

Từ khóa liên quan

Mục lục

  • Contents

  • Acknowledgments

  • Introduction

  • Chapter 1 The Development Environment

    • SQL Server Management Studio

      • The SQL Server Management Studio User Interface

      • SQL Server Management Studio User Interface Windows

      • SQL Server 2005 Administrative Tools

      • BI Development Studio

        • The Business Intelligence Development Studio User Interface

        • BI Development Studio User Interface Windows

        • Summary

        • Chapter 2 Developing with T-SQL

          • T-SQL Development Tools

            • SQL Server Management Studio

            • Visual Studio 2005

            • Creating Database Objects Using T-SQL DDL

              • Databases

              • Tables

              • Views

              • Synonyms

              • Stored Procedures

              • Functions

              • Triggers

              • Security

              • Storage for Searching

Tài liệu cùng người dùng

Tài liệu liên quan