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

📄 readerlist.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;

using Microsoft.Office.Interop.Excel;

namespace Library
{
	/// <summary>
	/// UserList 的摘要说明。
	/// </summary>
	public class ReaderList : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Button btnAddReader;
		private System.Windows.Forms.Button btnEditReader;
		private System.Windows.Forms.Button btnDeleteReader;
		private System.Windows.Forms.Button btnRefresh;
		private System.Windows.Forms.Button btnOutput;
		private System.Windows.Forms.Button btnClose;
		private System.Windows.Forms.ListView lvReaders;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		private bool reset = false;

		public ReaderList()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			initializeScreen();
		}

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

		/// <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
			{
				if(Global.conn.State!=ConnectionState.Open)
					Global.conn.Open();
				adapter.Fill(ds);
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.ToString());
			}
			return ds.Tables[0];
		}
		/// <summary>
		/// 初始化
		/// </summary>
		private void initializeScreen()
		{
			ListViewItem item = null;
			int colIndex;
			lvReaders.Items.Clear();
			System.Data.DataTable tableTemp = GetData("读者信息");
			if(!reset)
			{
				//将所得到的表的列名,赋值给单元格
				foreach(DataColumn col in tableTemp.Columns)
				{
					lvReaders.Columns.Add(col.ColumnName,-2,HorizontalAlignment.Left);
				}
			}
			//同样方法处理数据
			foreach(DataRow row in tableTemp.Rows)
			{
				item = new ListViewItem(row[0].ToString());
				for(colIndex= 1;colIndex<tableTemp.Columns.Count;colIndex++)
				{
					item.SubItems.Add(row[colIndex].ToString());
				}
				lvReaders.Items.Add(item);
			}
			tableTemp.Dispose();
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.btnClose = new System.Windows.Forms.Button();
			this.btnOutput = new System.Windows.Forms.Button();
			this.btnRefresh = new System.Windows.Forms.Button();
			this.btnDeleteReader = new System.Windows.Forms.Button();
			this.btnEditReader = new System.Windows.Forms.Button();
			this.btnAddReader = new System.Windows.Forms.Button();
			this.lvReaders = new System.Windows.Forms.ListView();
			this.groupBox1.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.Location = new System.Drawing.Point(283, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(216, 23);
			this.label1.TabIndex = 8;
			this.label1.Text = "读  者  管  理";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.btnClose);
			this.groupBox1.Controls.Add(this.btnOutput);
			this.groupBox1.Controls.Add(this.btnRefresh);
			this.groupBox1.Controls.Add(this.btnDeleteReader);
			this.groupBox1.Controls.Add(this.btnEditReader);
			this.groupBox1.Controls.Add(this.btnAddReader);
			this.groupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.groupBox1.Location = new System.Drawing.Point(24, 416);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(736, 72);
			this.groupBox1.TabIndex = 10;
			this.groupBox1.TabStop = false;
			// 
			// btnClose
			// 
			this.btnClose.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnClose.Location = new System.Drawing.Point(583, 24);
			this.btnClose.Name = "btnClose";
			this.btnClose.TabIndex = 5;
			this.btnClose.Text = "关闭";
			this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
			// 
			// btnOutput
			// 
			this.btnOutput.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnOutput.Location = new System.Drawing.Point(482, 24);
			this.btnOutput.Name = "btnOutput";
			this.btnOutput.TabIndex = 4;
			this.btnOutput.Text = "导出";
			this.btnOutput.Click += new System.EventHandler(this.btnOutput_Click);
			// 
			// btnRefresh
			// 
			this.btnRefresh.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnRefresh.Location = new System.Drawing.Point(381, 25);
			this.btnRefresh.Name = "btnRefresh";
			this.btnRefresh.TabIndex = 3;
			this.btnRefresh.Text = "刷新";
			this.btnRefresh.Click += new System.EventHandler(this.btnRefresh_Click);
			// 
			// btnDeleteReader
			// 
			this.btnDeleteReader.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnDeleteReader.Location = new System.Drawing.Point(280, 25);
			this.btnDeleteReader.Name = "btnDeleteReader";
			this.btnDeleteReader.TabIndex = 2;
			this.btnDeleteReader.Text = "删除";
			this.btnDeleteReader.Click += new System.EventHandler(this.btnDeleteReader_Click);
			// 
			// btnEditReader
			// 
			this.btnEditReader.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnEditReader.Location = new System.Drawing.Point(179, 25);
			this.btnEditReader.Name = "btnEditReader";
			this.btnEditReader.TabIndex = 1;
			this.btnEditReader.Text = "修改";
			// 
			// btnAddReader
			// 
			this.btnAddReader.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnAddReader.Location = new System.Drawing.Point(78, 24);
			this.btnAddReader.Name = "btnAddReader";
			this.btnAddReader.TabIndex = 0;
			this.btnAddReader.Text = "添加";
			this.btnAddReader.Click += new System.EventHandler(this.btnAddReader_Click);
			// 
			// lvReaders
			// 
			this.lvReaders.FullRowSelect = true;
			this.lvReaders.GridLines = true;
			this.lvReaders.Location = new System.Drawing.Point(24, 48);
			this.lvReaders.Name = "lvReaders";
			this.lvReaders.Size = new System.Drawing.Size(736, 360);
			this.lvReaders.TabIndex = 11;
			this.lvReaders.View = System.Windows.Forms.View.Details;
			// 
			// ReaderList
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(790, 503);
			this.ControlBox = false;
			this.Controls.Add(this.lvReaders);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.label1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
			this.Name = "ReaderList";
			this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
			this.Text = "读者管理";
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void btnDeleteReader_Click(object sender, System.EventArgs e)
		{
			if(lvReaders.SelectedItems.Count<=0)
			{
				MessageBox.Show("请选择需要删除的记录!可以多选!");
				return;
			}

			if(MessageBox.Show("您确定要删除被选的"+lvReaders.SelectedItems.Count.ToString() + "条记录?\n注意:此处并非真正删除读者记录,而是相当于注销借书证!","询问",MessageBoxButtons.YesNo)==DialogResult.Yes)
			{
				string sqlstr = "";
				for(int i=0;i<lvReaders.SelectedItems.Count;i++)
				{
					sqlstr = "update 借书证 set 借书证状态=1 where 借书证号='" + lvReaders.SelectedItems[i].SubItems[0].Text + "'";
					SqlCommand cmd = new SqlCommand(sqlstr,Global.conn);
					try
					{
						cmd.ExecuteNonQuery();
					}
					catch(Exception ex)
					{
						MessageBox.Show("删除记录时发生错误!错误信息:" + ex.Message);
						return;
					}
				}
				initializeScreen();

			}
		}

		private void btnOutput_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);

			System.Data.DataTable tableTemp = GetData("读者信息");
     
			//将所得到的表的列名,赋值给单元格
			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;
		}

		private void btnClose_Click(object sender, System.EventArgs e)
		{
			Global.sbpGlobal.Text = "就绪";
			this.Close();
		}

		private void btnRefresh_Click(object sender, System.EventArgs e)
		{
			reset = true;
			initializeScreen();
		}

		private void btnAddReader_Click(object sender, System.EventArgs e)
		{
			AddNewCard addNewCard = new AddNewCard();
			addNewCard.ShowDialog();
			reset = false;
			initializeScreen();
		}
	}
}

⌨️ 快捷键说明

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