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

📄 depositbookrack.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>
	/// DepositBookrack 的摘要说明。
	/// </summary>
	public class DepositBookrack : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.ListView listView1;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.Button btnChange;
		private System.Windows.Forms.Button btnSave;
		private System.Windows.Forms.Button btnAddNew;
		private System.Windows.Forms.Button btnDel;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		private SqlCommand cmd;
		private SqlDataReader dr;
		private bool isUpdate = false;

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

			listView1.Columns.Add("书架编号",100,HorizontalAlignment.Left);
			listView1.Columns.Add("书架名称",120,HorizontalAlignment.Left);
		}

		/// <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.groupBox1 = new System.Windows.Forms.GroupBox();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.listView1 = new System.Windows.Forms.ListView();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.btnSave = new System.Windows.Forms.Button();
			this.button3 = new System.Windows.Forms.Button();
			this.btnChange = new System.Windows.Forms.Button();
			this.btnAddNew = new System.Windows.Forms.Button();
			this.btnDel = new System.Windows.Forms.Button();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.textBox2);
			this.groupBox1.Controls.Add(this.textBox1);
			this.groupBox1.Controls.Add(this.label2);
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.groupBox1.Location = new System.Drawing.Point(16, 16);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(296, 104);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "基本设置";
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(92, 62);
			this.textBox2.Name = "textBox2";
			this.textBox2.ReadOnly = true;
			this.textBox2.Size = new System.Drawing.Size(176, 21);
			this.textBox2.TabIndex = 3;
			this.textBox2.Text = "";
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(92, 22);
			this.textBox1.Name = "textBox1";
			this.textBox1.ReadOnly = true;
			this.textBox1.Size = new System.Drawing.Size(176, 21);
			this.textBox1.TabIndex = 2;
			this.textBox1.Text = "";
			// 
			// label2
			// 
			this.label2.AutoSize = true;
			this.label2.Location = new System.Drawing.Point(28, 64);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(54, 17);
			this.label2.TabIndex = 1;
			this.label2.Text = "书架名称";
			// 
			// label1
			// 
			this.label1.AutoSize = true;
			this.label1.Location = new System.Drawing.Point(28, 24);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(54, 17);
			this.label1.TabIndex = 0;
			this.label1.Text = "书架编号";
			// 
			// listView1
			// 
			this.listView1.FullRowSelect = true;
			this.listView1.GridLines = true;
			this.listView1.Location = new System.Drawing.Point(16, 136);
			this.listView1.Name = "listView1";
			this.listView1.Size = new System.Drawing.Size(296, 152);
			this.listView1.TabIndex = 1;
			this.listView1.View = System.Windows.Forms.View.Details;
			this.listView1.Click += new System.EventHandler(this.listView1_Click);
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.Add(this.btnDel);
			this.groupBox2.Controls.Add(this.btnSave);
			this.groupBox2.Controls.Add(this.button3);
			this.groupBox2.Controls.Add(this.btnChange);
			this.groupBox2.Controls.Add(this.btnAddNew);
			this.groupBox2.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.groupBox2.Location = new System.Drawing.Point(16, 296);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(296, 64);
			this.groupBox2.TabIndex = 2;
			this.groupBox2.TabStop = false;
			// 
			// btnSave
			// 
			this.btnSave.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnSave.Location = new System.Drawing.Point(124, 24);
			this.btnSave.Name = "btnSave";
			this.btnSave.Size = new System.Drawing.Size(48, 23);
			this.btnSave.TabIndex = 3;
			this.btnSave.Text = "保存";
			this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
			// 
			// button3
			// 
			this.button3.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.button3.Location = new System.Drawing.Point(248, 24);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(40, 23);
			this.button3.TabIndex = 2;
			this.button3.Text = "关闭";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// btnChange
			// 
			this.btnChange.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnChange.Location = new System.Drawing.Point(70, 24);
			this.btnChange.Name = "btnChange";
			this.btnChange.Size = new System.Drawing.Size(40, 23);
			this.btnChange.TabIndex = 1;
			this.btnChange.Text = "更改";
			this.btnChange.Click += new System.EventHandler(this.btnChange_Click);
			// 
			// btnAddNew
			// 
			this.btnAddNew.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnAddNew.Location = new System.Drawing.Point(8, 24);
			this.btnAddNew.Name = "btnAddNew";
			this.btnAddNew.Size = new System.Drawing.Size(48, 23);
			this.btnAddNew.TabIndex = 0;
			this.btnAddNew.Text = "新增";
			this.btnAddNew.Click += new System.EventHandler(this.btnAddNew_Click);
			// 
			// btnDel
			// 
			this.btnDel.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnDel.Location = new System.Drawing.Point(186, 24);
			this.btnDel.Name = "btnDel";
			this.btnDel.Size = new System.Drawing.Size(48, 23);
			this.btnDel.TabIndex = 4;
			this.btnDel.Text = "删除";
			this.btnDel.Click += new System.EventHandler(this.btnDel_Click);
			// 
			// DepositBookrack
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(330, 376);
			this.ControlBox = false;
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.listView1);
			this.Controls.Add(this.groupBox1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.Name = "DepositBookrack";
			this.ShowInTaskbar = false;
			this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "图书存放";
			this.Load += new System.EventHandler(this.DepositBookrack_Load);
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion
		/// <summary>
		/// 初始化界面
		/// </summary>
		private void initializeScreen()
		{
			btnAddNew.Enabled = true;
			btnChange.Enabled = true;
			btnSave.Enabled = false;
			btnDel.Enabled = false;
			textBox2.ReadOnly = true;
			listView1.Items.Clear();
			ListViewItem item = null;
			string str = "select * from 存放位置";
			cmd.CommandText = str;
			try
			{
				dr = cmd.ExecuteReader();
				while(dr.Read())
				{
					item = new ListViewItem(dr.GetInt32(0).ToString());
					item.SubItems.Add(dr.GetString(1));
					listView1.Items.Add(item);
				}
				dr.Close();
				listView1.Focus();
			}
			catch(Exception ex)
			{
				if(dr != null)dr.Close();
				MessageBox.Show(ex.ToString());
				return;
			}
		}
		/// <summary>
		/// 窗体加载代码
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void DepositBookrack_Load(object sender, System.EventArgs e)
		{
			cmd = Global.conn.CreateCommand();
			initializeScreen();
		}
		/// <summary>
		/// 关闭按钮
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void button3_Click(object sender, System.EventArgs e)
		{
			Global.sbpGlobal.Text = "就绪";
			this.Close();
		}
		/// <summary>
		/// 新增书架
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnAddNew_Click(object sender, System.EventArgs e)
		{
			isUpdate = false;
			textBox1.Text = "";
			textBox1.ReadOnly = true;
			textBox2.Text = "";
			textBox2.ReadOnly = false;
			btnAddNew.Enabled = false;
			btnSave.Enabled = true;
			btnChange.Enabled = false;
			btnDel.Enabled = false;
			textBox2.Focus();
		}
		/// <summary>
		/// 记录项单击
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void listView1_Click(object sender, System.EventArgs e)
		{
			isUpdate = false;
			textBox1.ReadOnly = true;
			textBox2.ReadOnly = true;
			btnSave.Enabled = false;
			btnDel.Enabled = true;
			string str = "";
			if(listView1.SelectedItems[0].Text != "")
			{
				str = "select * from 存放位置 where 书架编号='" + listView1.SelectedItems[0].SubItems[0].Text + "'";
				cmd.CommandText = str;
				try
				{
					dr = cmd.ExecuteReader();
					dr.Read();
					if(dr.HasRows)
					{
						textBox1.Text = dr.GetInt32(0).ToString();
						textBox2.Text = dr.GetString(1);
					}
					dr.Close();
				}
				catch(Exception ex)
				{
					if(dr!= null)dr.Close();
					MessageBox.Show(ex.ToString() + "错误");
					return;
				}
			}
		}
		/// <summary>
		/// 删除记录
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnDel_Click(object sender, System.EventArgs e)
		{
			string str = "";
			if(listView1.SelectedItems[0].Text != "")
			{
				if(MessageBox.Show("所有存放在此书架上的图书的存放位置将被置空!确定删除?","询问",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
				{
					//创建事务
					SqlTransaction trans = Global.conn.BeginTransaction();
					cmd.Transaction = trans;
					try
					{
						str = "delete from 存放位置 where 书架编号=" + Convert.ToInt32(listView1.SelectedItems[0].Text);
						cmd.CommandText = str;
						cmd.ExecuteNonQuery();
						str = "update 图书资料 set 书架名称='' where 书架名称='" + listView1.SelectedItems[0].SubItems[1].Text + "'";
						cmd.CommandText = str;
						cmd.ExecuteNonQuery();
						trans.Commit();
					}
					catch
					{
						try
						{
							trans.Rollback();
						}
						catch (SqlException ex)
						{
							if (trans.Connection != null)
							{
								MessageBox.Show(ex.ToString());
							}
						}
						MessageBox.Show("发生错误!图书借阅未成功!");
						initializeScreen();
						return;
					}
					MessageBox.Show("书架删除成功!");
					initializeScreen();
				}
				else
				{
					listView1.Focus();
				}
			}
		}
		/// <summary>
		/// 更改
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnChange_Click(object sender, System.EventArgs e)
		{
			isUpdate = true;
			textBox1.ReadOnly = true;
			textBox2.ReadOnly = false;
			btnAddNew.Enabled = false;
			btnChange.Enabled = false;
			btnSave.Enabled = true;
			btnDel.Enabled = false;
			textBox2.Focus();
		}

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			if(textBox2.Text != "")
			{
				string str = "";
				if(isUpdate)
				{
					str = "select * from 存放位置 where 书架名称='" + textBox2.Text + "'";
					cmd.CommandText =str;
					try
					{
						dr = cmd.ExecuteReader();
						dr.Read();
						if(dr.HasRows)
						{
							if(dr!=null)dr.Close();
							MessageBox.Show("已经存在此书架名称了!");
							return;
						}
						if(dr!=null)dr.Close();
					}
					catch(Exception ex)
					{
						if(dr!=null)dr.Close();
						MessageBox.Show(ex.ToString());
						return;
					}
					str = "update 存放位置 set 书架名称='" + textBox2.Text + "' where 书架编号=" + Convert.ToInt32(textBox1.Text);
					cmd.CommandText = str;
					try
					{
						cmd.ExecuteNonQuery();
						MessageBox.Show("更改成功!");
						initializeScreen();
					}
					catch(Exception ex)
					{
						MessageBox.Show(ex.ToString());
						return;
					}
				}
				else
				{
					str = "select * from 存放位置 where 书架名称='" + textBox2.Text + "'";
					cmd.CommandText =str;
					try
					{
						dr = cmd.ExecuteReader();
						dr.Read();
						if(dr.HasRows)
						{
							if(dr!=null)dr.Close();
							MessageBox.Show("已经存在此书架名称了!");
							return;
						}
						if(dr!=null)dr.Close();
					}
					catch(Exception ex)
					{
						if(dr!=null)dr.Close();
						MessageBox.Show(ex.ToString());
						return;
					}
					str = "insert into 存放位置(书架名称) values('" + textBox2.Text + "')";
					cmd.CommandText = str;
					try
					{
						cmd.ExecuteNonQuery();
						MessageBox.Show("添加成功!");
						initializeScreen();
					}
					catch(Exception ex)
					{
						MessageBox.Show(ex.ToString());
						return;
					}
				}
			}
			else
			{
				MessageBox.Show("书架名称不能为空!","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning);
				return;
			}
		}
	}
}

⌨️ 快捷键说明

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