【FastCube.Net教程】如何將資料庫連線到多維資料集
通常,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上的”按鈕