CustomerAnalysisForm.cs
上传用户:abcs8778
上传日期:2022-08-03
资源大小:2093k
文件大小:8k
源码类别:
WEB源码(ASP,PHP,...)
开发平台:
SQL
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Data.SqlClient;
- using Microsoft.AnalysisServices.AdomdClient;
- namespace BookSellPredict
- {
- public partial class CustomerAnalysisForm : Form
- {
- protected SqlConnection _connDatabase;
- protected AdomdConnection _connAnalysis;
- protected AdomdCommand _cmd;
- public CustomerAnalysisForm(SqlConnection connectionDatabase, AdomdConnection connectionAnalysis)
- {
- _connDatabase = connectionDatabase;
- _connAnalysis = connectionAnalysis;
- _cmd = _connAnalysis.CreateCommand();
- InitializeComponent();
- }
- private void CustomerAnalysisForm_Load(object sender, EventArgs e)
- {
- SqlCommand cmd1 = new SqlCommand("SELECT DISTINCT [ID] FROM [Customer] ORDER BY [ID]", _connDatabase);
- SqlDataReader reader1 = cmd1.ExecuteReader();
- while (reader1.Read())
- cmbCustomerID.Items.Add(reader1[0]);
- reader1.Close();
- dgvLevels.AutoGenerateColumns = false;
- }
- private void btnLoadInfo_Click(object sender, EventArgs e)
- {
- if (cmbCustomerID.Text == "")
- {
- MessageBox.Show("会员编号不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- SqlCommand cmd1 = _connDatabase.CreateCommand();
- cmd1.CommandText = "SELECT [Birthday], [Area], [Degree], [Gender], [RegTime], [Levels], [Score] FROM [Customer] WHERE [ID]=" + cmbCustomerID.Text;
- SqlDataReader reader1 = cmd1.ExecuteReader();
- if (reader1.Read())
- {
- dtpBirthday.Value = (DateTime)reader1[0];
- if (reader1[1] != DBNull.Value)
- cmbArea.Text = reader1[1].ToString();
- if (reader1[2] != DBNull.Value)
- cmbDegree.Text = reader1[2].ToString();
- if ((bool)reader1[3])
- rdbMale.Checked = true;
- else
- rdbFemale.Checked = true;
- dtpRegTime.Value = (DateTime)reader1[4];
- cmbLevel.Text = reader1[5].ToString();
- nudScore.Value = (int)reader1[6];
- }
- reader1.Close();
- }
- private void btnPredictLevels_Click(object sender, EventArgs e)
- {
- if (cmbArea.Text == "" || cmbDegree.Text == "")
- {
- MessageBox.Show("地区或学历不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- int iAge = DateTime.Now.Year - dtpBirthday.Value.Year;
- _cmd.CommandText = string.Format("SELECT Predict([Levels]), PredictHistogram([Levels]) From [CustomerLevels] NATURAL PREDICTION JOIN (SELECT {0} AS [Age], '{1}' AS [Area], '{2}' AS [Degree], '{3}' AS [Gender]) AS t", iAge, cmbArea.Text, cmbDegree.Text, rdbMale.Checked);
- AdomdDataReader reader1 = _cmd.ExecuteReader();
- if (reader1.Read())
- {
- MessageBox.Show(string.Format("该会员最有可能成为{0}级会员", reader1[0]), "预测结果");
- AdomdDataReader reader2 = (AdomdDataReader)reader1[1];
- DataTable table1 = new DataTable();
- table1.Load(reader2);
- dgvLevels.DataSource = table1;
- }
- reader1.Close();
- }
- private void btnPredictBooks_Click(object sender, EventArgs e)
- {
- if (cmbArea.Text == "" || cmbDegree.Text == "")
- {
- MessageBox.Show("地区或学历不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- int iAge = DateTime.Now.Year - dtpBirthday.Value.Year;
- _cmd.CommandText = string.Format("SELECT Predict([BookSell], {0}) From [CustomerBooks] NATURAL PREDICTION JOIN (SELECT {1} AS [Age], '{2}' AS [Area], '{3}' AS [Degree], '{4}' AS [Gender]) AS t", nudNumber.Value, iAge, cmbArea.Text, cmbDegree.Text, rdbMale.Checked);
- AdomdDataReader reader1 = _cmd.ExecuteReader();
- if (reader1.Read())
- {
- AdomdDataReader reader2 = (AdomdDataReader)reader1[0];
- lbBooks.Items.Clear();
- while (reader2.Read())
- lbBooks.Items.Add(reader2[0]);
- reader2.Close();
- }
- reader1.Close();
- }
- private void btnPredictConsume_Click(object sender, EventArgs e)
- {
- if (cmbArea.Text == "" || cmbDegree.Text == "")
- {
- MessageBox.Show("地区或学历不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- int iAge = DateTime.Now.Year - dtpBirthday.Value.Year;
- _cmd.CommandText = string.Format("SELECT Predict([Dayconsume]), PredictHistogram([Dayconsume]) From [CustomerConsume] NATURAL PREDICTION JOIN (SELECT {0} AS [Age], '{1}' AS [Area], '{2}' AS [Degree], '{3}' AS [Gender]) AS t", iAge, cmbArea.Text, cmbDegree.Text, rdbMale.Checked);
- AdomdDataReader reader1 = _cmd.ExecuteReader();
- if (reader1.Read())
- {
- MessageBox.Show("该会员最有可能属于" + this.GetConsumeDescription((double)reader1[0]));
- AdomdDataReader reader2 = (AdomdDataReader)reader1[1];
- dgvConsume.Rows.Clear();
- double dConsume;
- while (reader2.Read())
- {
- if(reader2["Dayconsume"] != null && double.TryParse(reader2["Dayconsume"].ToString(), out dConsume))
- dgvConsume.Rows.Add(this.GetConsumeDescription(dConsume), reader2["$PROBABILITY"], reader2["$SUPPORT"]);
- }
- reader2.Close();
- }
- reader1.Close();
- }
- private void btnPredictType_Click(object sender, EventArgs e)
- {
- if (cmbDegree.Text == "")
- {
- MessageBox.Show("学历不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- cmbDegree.Focus();
- return;
- }
- int iAge = DateTime.Now.Year - dtpBirthday.Value.Year;
- int iRegDays = (DateTime.Now - dtpRegTime.Value).Days + 1;
- decimal dDayConsume = nudScore.Value / iRegDays;
- _cmd.CommandText = string.Format("SELECT Cluster(), PredictHistogram(Cluster()) From [CustomerType] NATURAL PREDICTION JOIN (SELECT {0} AS [Age], '{1}' AS [Degree], '{2}' AS [DayConsume]) AS t", iAge, cmbDegree.Text, dDayConsume);
- AdomdDataReader reader1 = _cmd.ExecuteReader();
- if (reader1.Read())
- {
- MessageBox.Show("该会员最有可能属于" + this.GetTypeDescription(reader1[0].ToString()));
- AdomdDataReader reader2 = (AdomdDataReader)reader1[1];
- dgvType.Rows.Clear();
- while (reader2.Read())
- {
- if (reader2["$CLUSTER"] != null)
- dgvType.Rows.Add(this.GetTypeDescription(reader2["$CLUSTER"].ToString()), reader2["$PROBABILITY"], reader2["$DISTANCE"]);
- }
- reader2.Close();
- }
- reader1.Close();
- }
- protected string GetConsumeDescription(double consume)
- {
- if (consume <= 5)
- return "偶然会员";
- else if (consume >= 15)
- return "忠实会员";
- else
- return "一般会员";
- }
- protected string GetTypeDescription(string type)
- {
- switch (type)
- {
- case "分类 1":
- return "偶然型读者";
- case "分类 2":
- return "科研型读者";
- case "分类 3":
- return "管理型读者";
- case "分类 4":
- return "学生型读者";
- default:
- return "其它类型";
- }
- }
- }
- }