通常,OLAP多維數據集中的數據是從數據庫加載的。要使用數據填充多維數據集,需要創建數據源,多維數據集可以接收以下數據:
Database(數據庫)—創建與數據庫的連接;
Stream(流)—可以通過網絡作爲流接收,從文件打開或從數據庫下載的多維數據集;
Application code(應用程序代碼:手動)—直接從應用程序代碼填充數據庫中的數據;
Cube file(多維數據集文件)— 數據已包含在多維數據集文件中以及數據方案中。
對於手動填充或從數據庫填充的多維數據集,還需要創建或加載其表示,例如:可以從mds文件加載現成的表示,在本文中將介紹將多維數據集連接到數據庫的方法。
帶有數據方案的文件
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <slice version="2" timestamp="12.05.2017 16:31:09"> <fields> <field name="OrderNo" caption="OrderNo" Width="0" Height="0" PopupWidth="120" PopupShowAvailable="False" Collapsed="False" CollapsedSubGroup="False"/> <field name="PartNo" caption="PartNo" Width="0" Height="0" PopupWidth="120" PopupShowAvailable="False" Collapsed="False" CollapsedSubGroup="False"/> <field name="Qty" caption="Qty" Width="0" Height="0" PopupWidth="120" PopupShowAvailable="False" Collapsed="False" CollapsedSubGroup="False"/> <field name="CustNo" caption="CustNo" Width="0" Height="0" PopupWidth="120" PopupShowAvailable="False" Collapsed="False" CollapsedSubGroup="False"/> <field name="EmpNo" caption="EmpNo" Width="0" Height="0" PopupWidth="120" PopupShowAvailable="False" Collapsed="False" CollapsedSubGroup="False"/> <field name="SaleDate" caption="SaleDate" Width="0" Height="0" PopupWidth="120" PopupShowAvailable="False" Collapsed="False" CollapsedSubGroup="False"/> </fields> <page_fields alignment="taLeftJustify"> <fields/> </page_fields> <xaxis_fields alignment="taLeftJustify" GrandTotalPosition="fctp_Before" UseGrandTotalPositionFromMeasure="False" DefaultTypeSort="md_tsa_ByAxisValue" AxisType="at_Standard"> <fields/> <AdditionalGrandTotals/> </xaxis_fields> <yaxis_fields alignment="taLeftJustify" GrandTotalPosition="fctp_Before" UseGrandTotalPositionFromMeasure="False" DefaultTypeSort="md_tsa_ByAxisValue" AxisType="at_Standard"> <fields> <field name="SaleDate" caption="SaleDate" alignment="taLeftJustify" captionwidth="100" field_name="SaleDate" SortDirection="fcsd_Asc" TotalPosition="fctp_Before" UseTotalPositionFromMeasure="False"> <AdditionalTotals/> </field> </fields> <AdditionalGrandTotals/> </yaxis_fields> <measure_fields alignment="taLeftJustify" caption="Measures" CaptionWidth="100" Region="X" Position="0"> <fields> <field name="Qty" caption="Qty" alignment="taRightJustify" captionwidth="100" field_name="Qty" field2_name="Qty" fieldadv_name="Qty" AgrFunc="af_Sum" Visible="True" DisplayAs="da_Value" CalcAllCells="False" CalcAllCellsForTotals="False" Distinct="False" ChangeNullToZero="False" DefaultTotalPosition="fctp_After" UseDifferentAggForTotals="False" AgrFuncForTotals="af_Sum" CalcTotalsOnTotals="False" UseXAxisTotalsAsBase="False"> <display_format Version="2" Name="General"/> <HIGHLIGHTS/> </field> </fields> </measure_fields> <topn/> <selection col="0" row="0" measure="0"/> <sortselection xlevelindex="0" xindexinlevel="0" xmeasureindex="0" xadditionaltotalindex="0" ylevelindex="0" yindexinlevel="0" ymeasureindex="0" yadditionaltotalindex="0"/> <options HideColZeros="False" HideRowZeros="False"/> <groups version="2" timestamp="12.05.2017 16:31:09"/> <filters version="2" timestamp="12.05.2017 16:31:09"/> <charts version="2" timestamp="12.05.2017 16:31:09"> <chart_properties TypeChartData="tcd_ByAxisMeasures" CategoriesAxis="ar_RowAxis" SeriesAxis="ar_ColAxis" CategoriesFieldCount="1" SeriesFieldCount="1" MeasureFieldIndex="0" MarksShowStyle="ssNone" SkipNullPoints="False" Legend_Visible="True"/> </charts> </slice>
這是一個簡單的XML文件,從數據庫中獲取的字段在部分中聲明,部分包含將在X軸上顯示的字段。類似地,僅針對Y軸的部分是相同的,X軸和Y軸都包含測量字段,具體取決於多維數據的方向,部分包含字段度量。文本將此方案加載到多維數據集中,並使用數據庫中的數據填充它。具體操作如下所示:創建一個WindowsForms應用程序,在項目引用中添加庫:FastReport.Olap,FastReport.Bars,在安裝了FastCube.Net的文件夾中找到:“C:\ Program Files(x86)\ FastReports \ FastCube.Net Professional”。然後將以下組件“拖”到工具箱中的表單:cube,dataSource,dbDataSet,slice,sliceGrid,oleDbConnection,oleDbCommand。
配置所有這些組件,連接到數據庫:在oleDbConnection1屬性中,將ConnectionString值設置爲“Provider = Microsoft.Jet.OLEDB.4.0; Data Source =”C:\ Program Files(x86)\ FastReports \ FastCube.Net Professional \ demo.mdb“。繼續執行組件oleDbCommand1,在其CommandText屬性中,編寫以下SQL查詢:
SELECT items.OrderNo, items.PartNo, items.Qty, orders.CustNo, orders.EmpNo, orders.SaleDate FROM (items LEFT OUTER JOIN orders ON items.OrderNo = orders.OrderNo) WHERE (items.OrderNo < 1100)
對於dbDataSet1組件,設置DbCommand屬性— oleDBCommand1;DataSource1,選擇DataSet— dbDataSet1;配置cube1,選擇DataSource —DataSource1,SourceType是一個DataSource。對於slice1組件,只需要設置一個屬性— cube(多維數據集),sliceGrid1組件是添加的唯一可視組件:
設置slice-slice1,爲表單創建一個OnLoad事件處理程序:
並添加以下代碼:
private void Form1_Load(object sender, EventArgs e) { string filePath = "J:/Program Files (x86)/FastReports/FastCube.Net Professional/Demos/Data/Cubes/dataset_en1.mds"; cube1.Load(filePath); cube1.Active = true; slice1.Load(filePath); }
加載一個多維數據和一個切片,必須激活多維數據集,以便從數據庫加載數據。現在啓動應用程序:
因此,將多維數據集連接到數據庫並不困難,可以通過手動創建跨表視圖(通過拖動字段並使用鼠標進行其他設置),然後使用slice.Save()方法或單擊“Save”來保存帶有方案的mds文件, “交叉標籤toolbar.scheme上的”按鈕