CustomerAnalysisForm.cs
上传用户:abcs8778
上传日期:2022-08-03
资源大小:2093k
文件大小:8k
源码类别:

WEB源码(ASP,PHP,...)

开发平台:

SQL

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Data.SqlClient;
  9. using Microsoft.AnalysisServices.AdomdClient;
  10. namespace BookSellPredict
  11. {
  12.     public partial class CustomerAnalysisForm : Form
  13.     {
  14.         protected SqlConnection _connDatabase;
  15.         protected AdomdConnection _connAnalysis;
  16.         protected AdomdCommand _cmd;
  17.         public CustomerAnalysisForm(SqlConnection connectionDatabase, AdomdConnection connectionAnalysis)
  18.         {
  19.             _connDatabase = connectionDatabase;
  20.             _connAnalysis = connectionAnalysis;
  21.             _cmd = _connAnalysis.CreateCommand();
  22.             InitializeComponent();
  23.         }
  24.         private void CustomerAnalysisForm_Load(object sender, EventArgs e)
  25.         {
  26.             SqlCommand cmd1 = new SqlCommand("SELECT DISTINCT [ID] FROM [Customer] ORDER BY [ID]", _connDatabase);
  27.             SqlDataReader reader1 = cmd1.ExecuteReader();
  28.             while (reader1.Read())
  29.                 cmbCustomerID.Items.Add(reader1[0]);
  30.             reader1.Close();
  31.             dgvLevels.AutoGenerateColumns = false;
  32.         }
  33.         private void btnLoadInfo_Click(object sender, EventArgs e)
  34.         {
  35.             if (cmbCustomerID.Text == "")
  36.             {
  37.                 MessageBox.Show("会员编号不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  38.                 return;
  39.             }
  40.             SqlCommand cmd1 = _connDatabase.CreateCommand();
  41.             cmd1.CommandText = "SELECT [Birthday], [Area], [Degree], [Gender], [RegTime], [Levels], [Score] FROM [Customer] WHERE [ID]=" + cmbCustomerID.Text;
  42.             SqlDataReader reader1 = cmd1.ExecuteReader();
  43.             if (reader1.Read())
  44.             {
  45.                 dtpBirthday.Value = (DateTime)reader1[0];
  46.                 if (reader1[1] != DBNull.Value)
  47.                     cmbArea.Text = reader1[1].ToString();
  48.                 if (reader1[2] != DBNull.Value)
  49.                     cmbDegree.Text = reader1[2].ToString();
  50.                 if ((bool)reader1[3])
  51.                     rdbMale.Checked = true;
  52.                 else
  53.                     rdbFemale.Checked = true;
  54.                 dtpRegTime.Value = (DateTime)reader1[4];
  55.                 cmbLevel.Text = reader1[5].ToString();
  56.                 nudScore.Value = (int)reader1[6];
  57.             }
  58.             reader1.Close();
  59.         }
  60.         private void btnPredictLevels_Click(object sender, EventArgs e)
  61.         {
  62.             if (cmbArea.Text == "" || cmbDegree.Text == "")
  63.             {
  64.                 MessageBox.Show("地区或学历不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  65.                 return;
  66.             }
  67.             int iAge = DateTime.Now.Year - dtpBirthday.Value.Year;
  68.             _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);
  69.             AdomdDataReader reader1 = _cmd.ExecuteReader();
  70.             if (reader1.Read())
  71.             {
  72.                 MessageBox.Show(string.Format("该会员最有可能成为{0}级会员", reader1[0]), "预测结果");
  73.                 AdomdDataReader reader2 = (AdomdDataReader)reader1[1];
  74.                 DataTable table1 = new DataTable();
  75.                 table1.Load(reader2);
  76.                 dgvLevels.DataSource = table1;
  77.             }
  78.             reader1.Close();
  79.         }
  80.         private void btnPredictBooks_Click(object sender, EventArgs e)
  81.         {
  82.             if (cmbArea.Text == "" || cmbDegree.Text == "")
  83.             {
  84.                 MessageBox.Show("地区或学历不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  85.                 return;
  86.             }
  87.             int iAge = DateTime.Now.Year - dtpBirthday.Value.Year;
  88.             _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);
  89.             AdomdDataReader reader1 = _cmd.ExecuteReader();
  90.             if (reader1.Read())
  91.             {
  92.                 AdomdDataReader reader2 = (AdomdDataReader)reader1[0];
  93.                 lbBooks.Items.Clear();
  94.                 while (reader2.Read())
  95.                     lbBooks.Items.Add(reader2[0]);
  96.                 reader2.Close();
  97.             }
  98.             reader1.Close();
  99.         }
  100.         private void btnPredictConsume_Click(object sender, EventArgs e)
  101.         {
  102.             if (cmbArea.Text == "" || cmbDegree.Text == "")
  103.             {
  104.                 MessageBox.Show("地区或学历不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  105.                 return;
  106.             }
  107.             int iAge = DateTime.Now.Year - dtpBirthday.Value.Year;
  108.             _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);
  109.             AdomdDataReader reader1 = _cmd.ExecuteReader();
  110.             if (reader1.Read())
  111.             {
  112.                 MessageBox.Show("该会员最有可能属于" + this.GetConsumeDescription((double)reader1[0]));
  113.                 AdomdDataReader reader2 = (AdomdDataReader)reader1[1];
  114.                 dgvConsume.Rows.Clear();
  115.                 double dConsume;
  116.                 while (reader2.Read())
  117.                 {
  118.                     if(reader2["Dayconsume"] != null && double.TryParse(reader2["Dayconsume"].ToString(), out dConsume))
  119.                         dgvConsume.Rows.Add(this.GetConsumeDescription(dConsume), reader2["$PROBABILITY"], reader2["$SUPPORT"]);
  120.                 }
  121.                 reader2.Close();
  122.             }
  123.             reader1.Close();
  124.         }
  125.         private void btnPredictType_Click(object sender, EventArgs e)
  126.         {
  127.             if (cmbDegree.Text == "")
  128.             {
  129.                 MessageBox.Show("学历不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  130.                 cmbDegree.Focus();
  131.                 return;
  132.             }
  133.             int iAge = DateTime.Now.Year - dtpBirthday.Value.Year;
  134.             int iRegDays = (DateTime.Now - dtpRegTime.Value).Days + 1;
  135.             decimal dDayConsume = nudScore.Value / iRegDays;
  136.             _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);
  137.             AdomdDataReader reader1 = _cmd.ExecuteReader();
  138.             if (reader1.Read())
  139.             {
  140.                 MessageBox.Show("该会员最有可能属于" + this.GetTypeDescription(reader1[0].ToString()));
  141.                 AdomdDataReader reader2 = (AdomdDataReader)reader1[1];
  142.                 dgvType.Rows.Clear();
  143.                 while (reader2.Read())
  144.                 {
  145.                     if (reader2["$CLUSTER"] != null)
  146.                         dgvType.Rows.Add(this.GetTypeDescription(reader2["$CLUSTER"].ToString()), reader2["$PROBABILITY"], reader2["$DISTANCE"]);
  147.                 }
  148.                 reader2.Close();
  149.             }
  150.             reader1.Close();
  151.         }
  152.         protected string GetConsumeDescription(double consume)
  153.         {
  154.             if (consume <= 5)
  155.                 return "偶然会员";
  156.             else if (consume >= 15)
  157.                 return "忠实会员";
  158.             else
  159.                 return "一般会员";
  160.         }
  161.         protected string GetTypeDescription(string type)
  162.         {
  163.             switch (type)
  164.             {
  165.                 case "分类 1":
  166.                     return "偶然型读者";
  167.                 case "分类 2":
  168.                     return "科研型读者";
  169.                 case "分类 3":
  170.                     return "管理型读者";
  171.                 case "分类 4":
  172.                     return "学生型读者";
  173.                 default:
  174.                     return "其它类型";
  175.             }
  176.         }
  177.     }
  178. }