⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 booksstatistics.cs

📁 图书馆信息管理系统 Library Information Management System
💻 CS
字号:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Library
{
	/// <summary>
	/// BooksStatistics 的摘要说明。
	/// </summary>
	public class BooksStatistics : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.ListView listView1;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button btnClose;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.ComboBox comboBox1;
		private System.Windows.Forms.RadioButton radioButton2;
		private System.Windows.Forms.RadioButton radioButton1;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button btnSearch;
		private System.Windows.Forms.TextBox textBox1;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;
		/// <summary>
		/// 临时数据表
		/// </summary>
		private static System.Data.DataTable tableTemp = null;
		/// <summary>
		/// 重置标志
		/// </summary>
		private bool reset = false;

		public BooksStatistics()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();
			//
			//初始化
			//
			initializeScreen();
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.listView1 = new System.Windows.Forms.ListView();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.button1 = new System.Windows.Forms.Button();
			this.btnClose = new System.Windows.Forms.Button();
			this.button3 = new System.Windows.Forms.Button();
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.comboBox1 = new System.Windows.Forms.ComboBox();
			this.radioButton2 = new System.Windows.Forms.RadioButton();
			this.radioButton1 = new System.Windows.Forms.RadioButton();
			this.label3 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.btnSearch = new System.Windows.Forms.Button();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.groupBox2.SuspendLayout();
			this.groupBox3.SuspendLayout();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.BackColor = System.Drawing.Color.WhiteSmoke;
			this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.label1.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.label1.ForeColor = System.Drawing.Color.Black;
			this.label1.Location = new System.Drawing.Point(290, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(216, 23);
			this.label1.TabIndex = 1;
			this.label1.Text = "图 书 借 阅 排 名";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// listView1
			// 
			this.listView1.FullRowSelect = true;
			this.listView1.GridLines = true;
			this.listView1.Location = new System.Drawing.Point(27, 56);
			this.listView1.Name = "listView1";
			this.listView1.Size = new System.Drawing.Size(736, 309);
			this.listView1.TabIndex = 2;
			this.listView1.View = System.Windows.Forms.View.Details;
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.Add(this.button1);
			this.groupBox2.Controls.Add(this.btnClose);
			this.groupBox2.Controls.Add(this.button3);
			this.groupBox2.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.groupBox2.Location = new System.Drawing.Point(480, 416);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(264, 72);
			this.groupBox2.TabIndex = 14;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "控制栏";
			// 
			// button1
			// 
			this.button1.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.button1.Location = new System.Drawing.Point(95, 32);
			this.button1.Name = "button1";
			this.button1.TabIndex = 4;
			this.button1.Text = "重置";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// btnClose
			// 
			this.btnClose.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnClose.Location = new System.Drawing.Point(178, 32);
			this.btnClose.Name = "btnClose";
			this.btnClose.TabIndex = 3;
			this.btnClose.Text = "关闭";
			this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
			// 
			// button3
			// 
			this.button3.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.button3.Location = new System.Drawing.Point(12, 32);
			this.button3.Name = "button3";
			this.button3.TabIndex = 2;
			this.button3.Text = "导出Excel";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.Add(this.comboBox1);
			this.groupBox3.Controls.Add(this.radioButton2);
			this.groupBox3.Controls.Add(this.radioButton1);
			this.groupBox3.Controls.Add(this.label3);
			this.groupBox3.Controls.Add(this.label2);
			this.groupBox3.Controls.Add(this.btnSearch);
			this.groupBox3.Controls.Add(this.textBox1);
			this.groupBox3.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.groupBox3.Location = new System.Drawing.Point(48, 376);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(416, 112);
			this.groupBox3.TabIndex = 13;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "查询记录";
			// 
			// comboBox1
			// 
			this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.comboBox1.Location = new System.Drawing.Point(104, 32);
			this.comboBox1.Name = "comboBox1";
			this.comboBox1.Size = new System.Drawing.Size(121, 20);
			this.comboBox1.TabIndex = 6;
			// 
			// radioButton2
			// 
			this.radioButton2.Location = new System.Drawing.Point(320, 32);
			this.radioButton2.Name = "radioButton2";
			this.radioButton2.Size = new System.Drawing.Size(72, 24);
			this.radioButton2.TabIndex = 9;
			this.radioButton2.Text = "模糊查询";
			// 
			// radioButton1
			// 
			this.radioButton1.Checked = true;
			this.radioButton1.Location = new System.Drawing.Point(240, 32);
			this.radioButton1.Name = "radioButton1";
			this.radioButton1.Size = new System.Drawing.Size(72, 24);
			this.radioButton1.TabIndex = 8;
			this.radioButton1.TabStop = true;
			this.radioButton1.Text = "精确查询";
			// 
			// label3
			// 
			this.label3.AutoSize = true;
			this.label3.Location = new System.Drawing.Point(24, 72);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(66, 17);
			this.label3.TabIndex = 7;
			this.label3.Text = "查询条件:";
			// 
			// label2
			// 
			this.label2.AutoSize = true;
			this.label2.Location = new System.Drawing.Point(24, 32);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(66, 17);
			this.label2.TabIndex = 5;
			this.label2.Text = "查询依据:";
			// 
			// btnSearch
			// 
			this.btnSearch.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnSearch.Location = new System.Drawing.Point(336, 72);
			this.btnSearch.Name = "btnSearch";
			this.btnSearch.Size = new System.Drawing.Size(56, 23);
			this.btnSearch.TabIndex = 4;
			this.btnSearch.Text = "查询";
			this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(104, 72);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(208, 21);
			this.textBox1.TabIndex = 3;
			this.textBox1.Text = "";
			// 
			// BooksStatistics
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(790, 503);
			this.ControlBox = false;
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.groupBox3);
			this.Controls.Add(this.listView1);
			this.Controls.Add(this.label1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
			this.Name = "BooksStatistics";
			this.ShowInTaskbar = false;
			this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
			this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
			this.Text = "图书借阅排名";
			this.groupBox2.ResumeLayout(false);
			this.groupBox3.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion
		/// <summary>
		/// 定义方法GetData(),返回一个数据表
		/// </summary>
		/// <returns>返回DataTable</returns>
		public static System.Data.DataTable GetData(string view)
		{
			SqlDataAdapter adapter = new SqlDataAdapter("select * from " + view,Global.conn);
			DataSet ds= new DataSet();
			try
			{
				adapter.Fill(ds);
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.ToString());
			}
			return ds.Tables[0];
		}
		/// <summary>
		/// 定义方法GetData(),返回一个数据表
		/// </summary>
		/// <returns>返回DataTable</returns>
		public static System.Data.DataTable GetData2(string sel)
		{
			SqlDataAdapter adapter = new SqlDataAdapter(sel,Global.conn);
			DataSet ds= new DataSet();
			try
			{
				adapter.Fill(ds);
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.ToString());
			}
			return ds.Tables[0];
		}
		/// <summary>
		/// 初始化
		/// </summary>
		private void initializeScreen()
		{
			ListViewItem item = null;
			int colIndex=0;
			tableTemp = GetData("V_图书借阅排名");
			if(reset)
			{
				listView1.Items.Clear();
				//将所得到的表的列名,赋值给单元格
				foreach(DataColumn col in tableTemp.Columns)
				{
					colIndex++;
					comboBox1.Items.Add(col.ColumnName);
				}
			}
			else
			{
				listView1.Clear();
				//将所得到的表的列名,赋值给单元格
				foreach(DataColumn col in tableTemp.Columns)
				{
					colIndex++;
					listView1.Columns.Add(col.ColumnName,-2,HorizontalAlignment.Left);
					comboBox1.Items.Add(col.ColumnName);
				}
			}
			//同样方法处理数据
			foreach(DataRow row in tableTemp.Rows)
			{
				colIndex=0;
				item = new ListViewItem(row[0].ToString());
				for(colIndex= 1;colIndex<tableTemp.Columns.Count;colIndex++)
				{
					item.SubItems.Add(row[colIndex].ToString());
				}
				listView1.Items.Add(item);
			}
		}
		/// <summary>
		/// 关闭按钮
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnClose_Click(object sender, System.EventArgs e)
		{
			Global.ActiveChild = "null";
			Global.BooksStatistics = false;
			this.Close();
		}
		/// <summary>
		/// 重置
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void button1_Click(object sender, System.EventArgs e)
		{
			reset = true;
			initializeScreen();
		}
		/// <summary>
		/// 将数据导出到Excel
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void button3_Click(object sender, System.EventArgs e)
		{
			Microsoft.Office.Interop.Excel.Application excel= new Microsoft.Office.Interop.Excel.Application();
			int rowIndex=1;
			int colIndex=0;
			excel.Application.Workbooks.Add(true);
     
			//将所得到的表的列名,赋值给单元格
			foreach(DataColumn col in tableTemp.Columns)
			{
				colIndex++; 
				excel.Cells[1,colIndex]=col.ColumnName;    
			}
			//同样方法处理数据
			foreach(DataRow row in tableTemp.Rows)
			{
				rowIndex++;
				colIndex=0;
				foreach(DataColumn col in tableTemp.Columns)
				{
					colIndex++;
					excel.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString();
				}
			}
			MessageBox.Show("成功导出记录!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
			//不可见,即后台处理
			excel.Visible=true;
		}
		/// <summary>
		/// 查询
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnSearch_Click(object sender, System.EventArgs e)
		{
			string str = "";
			if(comboBox1.Text != "")
			{
				if(radioButton1.Checked)
				{
					str = "select * from V_图书借阅排名"+ " where " + comboBox1.Text + "='" + textBox1.Text + "'";
					ListViewItem item = null;
					listView1.Items.Clear();
					int colIndex=0;
					tableTemp = GetData2(str);
					//同样方法处理数据
					foreach(DataRow row in tableTemp.Rows)
					{
						colIndex=0;
						item = new ListViewItem(row[0].ToString());
						for(colIndex= 1;colIndex<tableTemp.Columns.Count;colIndex++)
						{
							item.SubItems.Add(row[colIndex].ToString());
						}
						listView1.Items.Add(item);
					}
				}
				else
				{
					str = "select * from V_图书借阅排名" + " where " + comboBox1.Text + " like '%" + textBox1.Text + "%'";
					ListViewItem item = null;
					listView1.Items.Clear();
					int colIndex=0;
					tableTemp = GetData2(str);
					//同样方法处理数据
					foreach(DataRow row in tableTemp.Rows)
					{
						colIndex=0;
						item = new ListViewItem(row[0].ToString());
						for(colIndex= 1;colIndex<tableTemp.Columns.Count;colIndex++)
						{
							item.SubItems.Add(row[colIndex].ToString());
						}
						listView1.Items.Add(item);
					}
				}
			}
			else
			{
				MessageBox.Show("查询依据不清楚!请重新设定!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
				comboBox1.Focus();
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -