📄 dmmviewerdemo.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.AnalysisServices.AdomdClient;
namespace Chapter20WinClient
{
public partial class frmModelViewer : Form
{
// Generic viewr control object...will be assigned instance of a specific type:
Microsoft.AnalysisServices.Viewers.MiningModelViewerControl objViewer;
// Shared connection string variable, defaulted to localhost (".")
string strConnectionString = ".";
public frmModelViewer()
{
InitializeComponent();
}
private void cboMiningModels_SelectedIndexChanged(object sender, EventArgs e)
{
int intTop;
int intLeft;
int intHeight;
int intWidth;
// UI
Cursor.Current = Cursors.WaitCursor;
if (objViewer == null)
{
// No control to copy position and size from, therefore position
// at x=50, y=0 and size to form's client height -50 and client width
intTop = 50;
intLeft = 0;
intHeight = this.ClientRectangle.Height - intTop;
intWidth = this.ClientRectangle.Width;
}
else
{
// Copy position and size of old control
intTop = objViewer.Top;
intLeft = objViewer.Left;
intHeight = objViewer.Height;
intWidth = objViewer.Width;
Controls.Remove(objViewer);
}
// Open connection (required to determine selected model's algorithm)
AdomdConnection conn = new AdomdConnection(strConnectionString);
conn.Open();
// Determine mining mode to view:
MiningModel mm = conn.MiningModels[cboMiningModels.Text];
// Correlate control typeof with model's algorithm:
switch (mm.Algorithm)
{
case "Microsoft_Decision_Trees":
{
objViewer = new Microsoft.AnalysisServices.Viewers.TreeViewer();
}
break;
case "Microsoft_Clustering":
objViewer = new Microsoft.AnalysisServices.Viewers.ClusterViewer();
break;
case "Microsoft_Sequence_Clustering":
objViewer = new Microsoft.AnalysisServices.Viewers.SequenceClusterViewer();
break;
case "Microsoft_Naive_Bayes":
objViewer = new Microsoft.AnalysisServices.Viewers.NaiveBayesViewer();
break;
case "Microsoft_Time_Series":
objViewer = new Microsoft.AnalysisServices.Viewers.TimeSeriesViewer();
break;
case "Microsoft_Association_Rules":
objViewer = new Microsoft.AnalysisServices.Viewers.AssociationViewer();
break;
case "Microsoft_Neural_Network":
objViewer = new Microsoft.AnalysisServices.Viewers.NeuralNetViewer();
break;
}
// close connection:
conn.Close();
// Add the control to the form's controls collection
Controls.Add(objViewer);
// Set size/position using saved settings and anchor
objViewer.Top = intTop;
objViewer.Left = intLeft;
objViewer.Height= intHeight;
objViewer.Width = intWidth;
objViewer.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right;
// Connect viewer to selected database and model
objViewer.ConnectionString = "Provider=MSOLAP.3;" + strConnectionString;
objViewer.MiningModelName = cboMiningModels.Text;
// Load the model content into the viewer
objViewer.LoadViewerData(null);
// UI
objViewer.Enabled = true;
Cursor.Current = Cursors.Default;
}
private void btnGetDatabases_Click(object sender, EventArgs e)
{
// Get catalogs on selected server using GetSchemaDataSet
cboCatalogs.Items.Clear();
AdomdConnection conn = new AdomdConnection("Data Source=" + txtServer.Text);
try
{
conn.Open();
foreach (DataRow dr in conn.GetSchemaDataSet(AdomdSchemaGuid.Catalogs, null).Tables[0].Rows)
{
cboCatalogs.Items.Add((string)dr[0]);
}
lblDatabases.Enabled = true;
cboCatalogs.Visible = true;
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
lblDatabases.Enabled = true;
cboCatalogs.Enabled = true;
lblMiningModel.Enabled = false;
cboMiningModels.Enabled = false;
}
private void cboCatalogs_SelectedIndexChanged(object sender, EventArgs e)
{
// Set strConnectionString according to slection
strConnectionString = "Data Source=" + txtServer.Text + ";Initial Catalog=" + cboCatalogs.Text;
// Open connecting using strConnectionString
AdomdConnection conn = new AdomdConnection(strConnectionString);
conn.Open();
// Populate cboMiningModels with content of conn.MiningModels
cboMiningModels.Items.Clear();
foreach (MiningModel mm in conn.MiningModels)
{
cboMiningModels.Items.Add(mm.Name);
}
// Close connection
conn.Close();
// UI
lblMiningModel.Enabled = true;
cboMiningModels.Enabled = true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -