BookAnalysisForm.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 BookAnalysisForm : Form
  13.     {
  14.         protected SqlConnection _connDatabase;
  15.         protected AdomdConnection _connAnalysis;
  16.         protected AdomdCommand _cmd;
  17.         public BookAnalysisForm(SqlConnection connectionDatabase, AdomdConnection connectionAnalysis)
  18.         {
  19.             _connDatabase = connectionDatabase;
  20.             _connAnalysis = connectionAnalysis;
  21.             _cmd = _connAnalysis.CreateCommand();
  22.             InitializeComponent();
  23.         }
  24.         private void BookAnalysisForm_Load(object sender, EventArgs e)
  25.         {
  26.             SqlCommand cmd1 = new SqlCommand("SELECT DISTINCT [ID] FROM [Book] ORDER BY [ID]", _connDatabase);
  27.             SqlDataReader reader1 = cmd1.ExecuteReader();
  28.             while (reader1.Read())
  29.                 cmbBookID.Items.Add(reader1[0]);
  30.             reader1.Close();
  31.             cmd1.CommandText = "SELECT DISTINCT [Category] FROM [BookCategory]";
  32.             reader1 = cmd1.ExecuteReader();
  33.             while (reader1.Read())
  34.                 cmbCategory.Items.Add(reader1[0]);
  35.             reader1.Close();
  36.             cmd1.CommandText = "SELECT DISTINCT [Press] FROM [Book]";
  37.             reader1 = cmd1.ExecuteReader();
  38.             while (reader1.Read())
  39.                 cmbPress.Items.Add(reader1[0]);
  40.             reader1.Close();
  41.         }
  42.         private void btnLoadInfo_Click(object sender, EventArgs e)
  43.         {
  44.             if (cmbBookID.Text == "")
  45.             {
  46.                 MessageBox.Show("图书编号不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  47.                 return;
  48.             }
  49.             SqlCommand cmd1 = _connDatabase.CreateCommand();
  50.             cmd1.CommandText = "SELECT [Name], [Category], [Press], [PublishDate], [ShelfDate], [Pages], [Price], [CD] FROM [Book], [BookCategory] WHERE [Book].[CategoryID]=[BookCategory].[ID] AND [Book].[ID]=" + cmbBookID.Text;
  51.             SqlDataReader reader1 = cmd1.ExecuteReader();
  52.             if (reader1.Read())
  53.             {
  54.                 tbBookName.Text = reader1[0].ToString();
  55.                 cmbCategory.Text = reader1[1].ToString();
  56.                 cmbPress.Text = reader1[2].ToString();
  57.                 if(reader1[3] != DBNull.Value)
  58.                     dtpPublishDate.Value = (DateTime)reader1[3];
  59.                 if (reader1[4] != DBNull.Value)
  60.                     dtpShelfDate.Value = (DateTime)reader1[4];
  61.                 if (reader1[5] != DBNull.Value)
  62.                     nudPages.Value = (short)reader1[5];
  63.                 if (reader1[6] != DBNull.Value)
  64.                     nudPrice.Value = (decimal)reader1[6];
  65.                 if (reader1[7] != DBNull.Value)
  66.                 {
  67.                     if ((bool)reader1[7])
  68.                         rdbCD.Checked = true;
  69.                     else
  70.                         rdbNoCD.Checked = true;
  71.                 }
  72.             }
  73.             reader1.Close();
  74.         }
  75.         private void btnPredictSellSeries_Click(object sender, EventArgs e)
  76.         {
  77.             if (cmbBookID.Text == "")
  78.             {
  79.                 MessageBox.Show("图书编号不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  80.                 return;
  81.             }
  82.             _cmd.CommandText = string.Format("SELECT PredictTimeSeries([SellSum], {0}) From [WeekSell] WHERE [ID]={1}", nudWeeks.Value, cmbBookID.Text);
  83.             AdomdDataReader reader1 = _cmd.ExecuteReader();
  84.             if (reader1.Read())
  85.             {
  86.                 AdomdDataReader reader2 = (AdomdDataReader)reader1[0];
  87.                 lvSellSum.Columns.Clear();
  88.                 lvSellSum.Items.Clear();
  89.                 lvSellSum.Columns.Add("星期");
  90.                 ListViewItem item1 = lvSellSum.Items.Add("销售额");
  91.                 while (reader2.Read())
  92.                 {
  93.                     if (reader2[0] != null && reader2[1] != null)
  94.                     {
  95.                         lvSellSum.Columns.Add(reader2[0].ToString());
  96.                         item1.SubItems.Add(string.Format("{0:N2}", reader2[1]));
  97.                     }
  98.                 }
  99.                 reader2.Close();
  100.             }
  101.             reader1.Close();
  102.         }
  103.         private void btnPredictSell_Click(object sender, EventArgs e)
  104.         {
  105.             if (cmbCategory.Text == "" || cmbPress.Text == "")
  106.             {
  107.                 MessageBox.Show("所属领域或出版社不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  108.                 return;
  109.             }
  110.             _cmd.CommandText = string.Format("SELECT PredictHistogram([DaySellNumber]) From [BookSell] NATURAL PREDICTION JOIN (SELECT '{0}' AS [Category], '{1}' AS [Press], {2} AS [Pages], {3} AS [Price], '{4}' AS [Gender]) AS t", cmbCategory.Text, cmbPress.Text, nudPages.Value, nudPrice.Value, rdbCD.Checked);
  111.             AdomdDataReader reader1 = _cmd.ExecuteReader();
  112.             if (reader1.Read())
  113.             {
  114.                 AdomdDataReader reader2 = (AdomdDataReader)reader1[0];
  115.                 if (reader2.Read() && reader2["DaySellNumber"] != null)
  116.                 {
  117.                     StringBuilder sb1 = new StringBuilder("预计该书日均销量:");
  118.                     sb1.Append(string.Format("{0:N2}册", reader2["DaySellNumber"]));
  119.                     sb1.Append(", 支持概率:");
  120.                     sb1.Append(string.Format("{0:F5}", reader2["$PROBABILITY"]));
  121.                     sb1.Append(", 支持计数:");
  122.                     sb1.Append(string.Format("{0:F5}", reader2["$SUPPORT"]));
  123.                     lbSellNum.Text = sb1.ToString();
  124.                 }
  125.                 reader2.Close();
  126.             }
  127.             reader1.Close();
  128.         }
  129.         private void btnPredictAssociation_Click(object sender, EventArgs e)
  130.         {
  131.             if (cmbBookID.Text == "" || tbBookName.Text == "")
  132.             {
  133.                 MessageBox.Show("图书编号和名称不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  134.                 return;
  135.             }
  136.             _cmd.CommandText = string.Format("SELECT PredictSequence([BookSell], {0}) From [BookAssociation] NATURAL PREDICTION JOIN (SELECT (SELECT {1} AS [Book ID], '{2}' AS [Name]) AS [BookSell]) AS t", nudNumber.Value, cmbBookID.Text, tbBookName.Text);
  137.             AdomdDataReader reader1 = _cmd.ExecuteReader();
  138.             if (reader1.Read())
  139.             {
  140.                 AdomdDataReader reader2 = (AdomdDataReader)reader1[0];
  141.                 lbBooks.Items.Clear();
  142.                 while (reader2.Read())
  143.                 {
  144.                     if(reader2["Name"] != null)
  145.                         lbBooks.Items.Add(reader2["Name"]);
  146.                 }
  147.                 reader2.Close();
  148.             }
  149.             reader1.Close();
  150.         }
  151.         private void btnPredictPrice_Click(object sender, EventArgs e)
  152.         {
  153.             _cmd.CommandText = string.Format("SELECT Predict([Price]), PredictProbability([Price]), PredictStdev([Price]) From [BookPrice] NATURAL PREDICTION JOIN (SELECT {0} AS [Pages]) AS t", nudPages.Value);
  154.             AdomdDataReader reader1 = _cmd.ExecuteReader();
  155.             if (reader1.Read() && reader1[0] != null)
  156.             {
  157.                 StringBuilder sb1 = new StringBuilder("该书的推荐定价为:");
  158.                 sb1.Append(string.Format("{0:C}±{1:N2}元,", reader1[0], reader1[2]));
  159.                 sb1.Append(string.Format("支持概率{0:F5}", reader1[1]));
  160.                 lbPrice.Text = sb1.ToString();
  161.             }
  162.             reader1.Close();
  163.         }
  164.     }
  165. }