BookAnalysisForm.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 BookAnalysisForm : Form
- {
- protected SqlConnection _connDatabase;
- protected AdomdConnection _connAnalysis;
- protected AdomdCommand _cmd;
- public BookAnalysisForm(SqlConnection connectionDatabase, AdomdConnection connectionAnalysis)
- {
- _connDatabase = connectionDatabase;
- _connAnalysis = connectionAnalysis;
- _cmd = _connAnalysis.CreateCommand();
- InitializeComponent();
- }
- private void BookAnalysisForm_Load(object sender, EventArgs e)
- {
- SqlCommand cmd1 = new SqlCommand("SELECT DISTINCT [ID] FROM [Book] ORDER BY [ID]", _connDatabase);
- SqlDataReader reader1 = cmd1.ExecuteReader();
- while (reader1.Read())
- cmbBookID.Items.Add(reader1[0]);
- reader1.Close();
- cmd1.CommandText = "SELECT DISTINCT [Category] FROM [BookCategory]";
- reader1 = cmd1.ExecuteReader();
- while (reader1.Read())
- cmbCategory.Items.Add(reader1[0]);
- reader1.Close();
- cmd1.CommandText = "SELECT DISTINCT [Press] FROM [Book]";
- reader1 = cmd1.ExecuteReader();
- while (reader1.Read())
- cmbPress.Items.Add(reader1[0]);
- reader1.Close();
- }
- private void btnLoadInfo_Click(object sender, EventArgs e)
- {
- if (cmbBookID.Text == "")
- {
- MessageBox.Show("图书编号不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- SqlCommand cmd1 = _connDatabase.CreateCommand();
- cmd1.CommandText = "SELECT [Name], [Category], [Press], [PublishDate], [ShelfDate], [Pages], [Price], [CD] FROM [Book], [BookCategory] WHERE [Book].[CategoryID]=[BookCategory].[ID] AND [Book].[ID]=" + cmbBookID.Text;
- SqlDataReader reader1 = cmd1.ExecuteReader();
- if (reader1.Read())
- {
- tbBookName.Text = reader1[0].ToString();
- cmbCategory.Text = reader1[1].ToString();
- cmbPress.Text = reader1[2].ToString();
- if(reader1[3] != DBNull.Value)
- dtpPublishDate.Value = (DateTime)reader1[3];
- if (reader1[4] != DBNull.Value)
- dtpShelfDate.Value = (DateTime)reader1[4];
- if (reader1[5] != DBNull.Value)
- nudPages.Value = (short)reader1[5];
- if (reader1[6] != DBNull.Value)
- nudPrice.Value = (decimal)reader1[6];
- if (reader1[7] != DBNull.Value)
- {
- if ((bool)reader1[7])
- rdbCD.Checked = true;
- else
- rdbNoCD.Checked = true;
- }
- }
- reader1.Close();
- }
- private void btnPredictSellSeries_Click(object sender, EventArgs e)
- {
- if (cmbBookID.Text == "")
- {
- MessageBox.Show("图书编号不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- _cmd.CommandText = string.Format("SELECT PredictTimeSeries([SellSum], {0}) From [WeekSell] WHERE [ID]={1}", nudWeeks.Value, cmbBookID.Text);
- AdomdDataReader reader1 = _cmd.ExecuteReader();
- if (reader1.Read())
- {
- AdomdDataReader reader2 = (AdomdDataReader)reader1[0];
- lvSellSum.Columns.Clear();
- lvSellSum.Items.Clear();
- lvSellSum.Columns.Add("星期");
- ListViewItem item1 = lvSellSum.Items.Add("销售额");
- while (reader2.Read())
- {
- if (reader2[0] != null && reader2[1] != null)
- {
- lvSellSum.Columns.Add(reader2[0].ToString());
- item1.SubItems.Add(string.Format("{0:N2}", reader2[1]));
- }
- }
- reader2.Close();
- }
- reader1.Close();
- }
- private void btnPredictSell_Click(object sender, EventArgs e)
- {
- if (cmbCategory.Text == "" || cmbPress.Text == "")
- {
- MessageBox.Show("所属领域或出版社不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- _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);
- AdomdDataReader reader1 = _cmd.ExecuteReader();
- if (reader1.Read())
- {
- AdomdDataReader reader2 = (AdomdDataReader)reader1[0];
- if (reader2.Read() && reader2["DaySellNumber"] != null)
- {
- StringBuilder sb1 = new StringBuilder("预计该书日均销量:");
- sb1.Append(string.Format("{0:N2}册", reader2["DaySellNumber"]));
- sb1.Append(", 支持概率:");
- sb1.Append(string.Format("{0:F5}", reader2["$PROBABILITY"]));
- sb1.Append(", 支持计数:");
- sb1.Append(string.Format("{0:F5}", reader2["$SUPPORT"]));
- lbSellNum.Text = sb1.ToString();
- }
- reader2.Close();
- }
- reader1.Close();
- }
- private void btnPredictAssociation_Click(object sender, EventArgs e)
- {
- if (cmbBookID.Text == "" || tbBookName.Text == "")
- {
- MessageBox.Show("图书编号和名称不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- _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);
- AdomdDataReader reader1 = _cmd.ExecuteReader();
- if (reader1.Read())
- {
- AdomdDataReader reader2 = (AdomdDataReader)reader1[0];
- lbBooks.Items.Clear();
- while (reader2.Read())
- {
- if(reader2["Name"] != null)
- lbBooks.Items.Add(reader2["Name"]);
- }
- reader2.Close();
- }
- reader1.Close();
- }
- private void btnPredictPrice_Click(object sender, EventArgs e)
- {
- _cmd.CommandText = string.Format("SELECT Predict([Price]), PredictProbability([Price]), PredictStdev([Price]) From [BookPrice] NATURAL PREDICTION JOIN (SELECT {0} AS [Pages]) AS t", nudPages.Value);
- AdomdDataReader reader1 = _cmd.ExecuteReader();
- if (reader1.Read() && reader1[0] != null)
- {
- StringBuilder sb1 = new StringBuilder("该书的推荐定价为:");
- sb1.Append(string.Format("{0:C}±{1:N2}元,", reader1[0], reader1[2]));
- sb1.Append(string.Format("支持概率{0:F5}", reader1[1]));
- lbPrice.Text = sb1.ToString();
- }
- reader1.Close();
- }
- }
- }