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

📄 loginsystem.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 Library.Class;

namespace Library
{
	/// <summary>
	/// LoginSystem 的摘要说明。
	/// </summary>
	public class LoginSystem : System.Windows.Forms.Form
	{
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.PictureBox pictureBox3;
		private System.Windows.Forms.PictureBox pictureBox2;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.Button btnOK;
		private System.Windows.Forms.TextBox txtPassword;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.ComboBox cmbUserName;
		private System.ComponentModel.IContainer components;

		private SqlCommand cmd ;
		private SqlDataReader dr;
		private Library.Class.HashMethod hm = new HashMethod();
		private bool IsLogin = true;

		public LoginSystem(bool isLogin)
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();
			//
			//
			//
			IsLogin = isLogin;
			if(isLogin == false)
			{
				cmbUserName.Text = Global.UserName;
				cmbUserName.Enabled = false;
			}
		}

		/// <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()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(LoginSystem));
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.pictureBox3 = new System.Windows.Forms.PictureBox();
			this.pictureBox2 = new System.Windows.Forms.PictureBox();
			this.btnCancel = new System.Windows.Forms.Button();
			this.btnOK = new System.Windows.Forms.Button();
			this.txtPassword = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.cmbUserName = new System.Windows.Forms.ComboBox();
			this.SuspendLayout();
			// 
			// pictureBox1
			// 
			this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Top;
			this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
			this.pictureBox1.Location = new System.Drawing.Point(0, 0);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(283, 38);
			this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
			this.pictureBox1.TabIndex = 1;
			this.pictureBox1.TabStop = false;
			this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
			// 
			// pictureBox3
			// 
			this.pictureBox3.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox3.Image")));
			this.pictureBox3.Location = new System.Drawing.Point(41, 108);
			this.pictureBox3.Name = "pictureBox3";
			this.pictureBox3.Size = new System.Drawing.Size(7, 9);
			this.pictureBox3.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
			this.pictureBox3.TabIndex = 19;
			this.pictureBox3.TabStop = false;
			// 
			// pictureBox2
			// 
			this.pictureBox2.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox2.Image")));
			this.pictureBox2.Location = new System.Drawing.Point(41, 56);
			this.pictureBox2.Name = "pictureBox2";
			this.pictureBox2.Size = new System.Drawing.Size(7, 9);
			this.pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
			this.pictureBox2.TabIndex = 18;
			this.pictureBox2.TabStop = false;
			// 
			// btnCancel
			// 
			this.btnCancel.BackColor = System.Drawing.Color.DodgerBlue;
			this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnCancel.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.btnCancel.Font = new System.Drawing.Font("华文细黑", 8.999999F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.btnCancel.Location = new System.Drawing.Point(169, 160);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(40, 21);
			this.btnCancel.TabIndex = 3;
			this.btnCancel.Text = "取消";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// btnOK
			// 
			this.btnOK.BackColor = System.Drawing.Color.DodgerBlue;
			this.btnOK.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.btnOK.Font = new System.Drawing.Font("华文细黑", 8.999999F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.btnOK.Location = new System.Drawing.Point(73, 160);
			this.btnOK.Name = "btnOK";
			this.btnOK.Size = new System.Drawing.Size(40, 21);
			this.btnOK.TabIndex = 2;
			this.btnOK.Text = "登陆";
			this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
			// 
			// txtPassword
			// 
			this.txtPassword.BackColor = System.Drawing.Color.DodgerBlue;
			this.txtPassword.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.txtPassword.Location = new System.Drawing.Point(65, 128);
			this.txtPassword.Name = "txtPassword";
			this.txtPassword.PasswordChar = '*';
			this.txtPassword.Size = new System.Drawing.Size(176, 14);
			this.txtPassword.TabIndex = 0;
			this.txtPassword.Text = "";
			// 
			// label2
			// 
			this.label2.AutoSize = true;
			this.label2.Font = new System.Drawing.Font("GulimChe", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label2.Location = new System.Drawing.Point(65, 104);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(49, 16);
			this.label2.TabIndex = 17;
			this.label2.Text = "Password";
			// 
			// label1
			// 
			this.label1.AutoSize = true;
			this.label1.Font = new System.Drawing.Font("GulimChe", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label1.Location = new System.Drawing.Point(65, 52);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(49, 16);
			this.label1.TabIndex = 16;
			this.label1.Text = "UserName";
			// 
			// cmbUserName
			// 
			this.cmbUserName.BackColor = System.Drawing.Color.DodgerBlue;
			this.cmbUserName.Location = new System.Drawing.Point(64, 80);
			this.cmbUserName.Name = "cmbUserName";
			this.cmbUserName.Size = new System.Drawing.Size(176, 20);
			this.cmbUserName.TabIndex = 1;
			// 
			// LoginSystem
			// 
			this.AcceptButton = this.btnOK;
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.BackColor = System.Drawing.Color.White;
			this.CancelButton = this.btnCancel;
			this.ClientSize = new System.Drawing.Size(278, 198);
			this.ControlBox = false;
			this.Controls.Add(this.cmbUserName);
			this.Controls.Add(this.pictureBox3);
			this.Controls.Add(this.pictureBox2);
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.btnOK);
			this.Controls.Add(this.txtPassword);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.pictureBox1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
			this.MaximizeBox = false;
			this.Name = "LoginSystem";
			this.ShowInTaskbar = false;
			this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "登陆";
			this.TransparencyKey = System.Drawing.Color.Red;
			this.Load += new System.EventHandler(this.LoginSystem_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void LoginSystem_Load(object sender, System.EventArgs e)
		{
			string str = "select 名称 from 管理员";
			cmd = Library.Global.conn.CreateCommand();
			cmd.CommandText = str;
			try
			{
				dr = cmd.ExecuteReader();
				while(dr.Read())
				{
					cmbUserName.Items.Add(dr.GetString(0));
				}
				cmbUserName.Text = cmbUserName.Items[0].ToString();
				dr.Close();
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.ToString());
				this.Close();
				Application.Exit();
			}
			txtPassword.Focus();
		}
		/// <summary>
		/// 登陆检测
		/// </summary>
		/// <param name="username">用户名</param>
		/// <param name="password">密码</param>
		private void LoginCheck(string username,string password)
		{
			string str = "select * from 管理员 where 名称='" + username + "'";
			cmd.CommandText = str;
			try
			{
				dr = cmd.ExecuteReader();
				dr.Read();
				if(dr.HasRows)
				{
					if(password == dr.GetString(8))
					{
						Global.UserName = username;
						if(dr.GetInt32(2) == 1)
						{
							Global.b_SystemSetting = true;
						}
						else
						{
							Global.b_SystemSetting = false;
						}
						if(dr.GetInt32(3) == 1)
						{
							Global.b_BooksSetting = true;
						}
						else
						{
							Global.b_BooksSetting = false;
						}
						if(dr.GetInt32(4) == 1)
						{
							Global.b_BooksManage = true;
						}
						else
						{
							Global.b_BooksManage = false;
						}
						if(dr.GetInt32(5) == 1)
						{
							Global.b_ReaderManage = true;
						}
						else
						{
							Global.b_ReaderManage = false;
						}
						if(dr.GetInt32(6) == 1)
						{
							Global.b_SystemSearch = true;
						}
						else
						{
							Global.b_SystemSearch = false;
						}
						if(dr.GetInt32(7) == 1)
						{
							Global.b_Statistics = true;
						}
						else
						{
							Global.b_Statistics = false;
						}
						if(dr != null) dr.Close();
						this.Close();
					}
					else
					{
						if(dr != null) dr.Close();
						MessageBox.Show("密码错误!","警告");
						txtPassword.Text = "";
						txtPassword.Focus();
					}
				}
				else
				{
					if(dr != null) dr.Close();
					MessageBox.Show("用户名错误!","警告");
					cmbUserName.Text = "";
					txtPassword.Text = "";
					cmbUserName.Focus();
				}
			}
			catch(Exception ex)
			{
				if(dr != null) dr.Close();
				MessageBox.Show(ex.ToString());
				this.Close();
				Application.Exit();
			}
		}
		/// <summary>
		/// 验证用户输入是否合法,即是否包含不合法字符
		/// </summary>
		/// <param name="validchars"></param>
		/// <param name="userinput"></param>
		/// <returns></returns>
		private bool ValidateInput(String validchars, String userinput)
		{
			foreach(Char c in userinput)
			{
				if(validchars.IndexOf(c) < 0)
				{
					return false;
				}
			}
			return true;
		}
		/// <summary>
		/// 检查字符串
		/// </summary>
		/// <param name="str_01"></param>
		/// <param name="str_02"></param>
		/// <returns></returns>
		private void CheckString(string str_01,string str_02)
		{
			//vChars提供字符验证所需的字符
			String vChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz*&%$#!";
			if(ValidateInput(vChars, str_01))
			{
				if(ValidateInput(vChars,str_02))
				{
					LoginCheck(str_01,hm.Encrypto(str_02));
				}
				else
				{
					MessageBox.Show("密码非法!可能是含有非法字符串。","错误");
					txtPassword.Text = "";
					txtPassword.Focus();
				}
			}
			else
			{
				MessageBox.Show("用户名非法!可能是含有非法字符串。","错误");
				cmbUserName.Text = "";
				cmbUserName.Focus();
			}
		}
		/// <summary>
		/// 登陆按钮
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnOK_Click(object sender, System.EventArgs e)
		{
			string um = cmbUserName.Text;
			string ps = txtPassword.Text;
			CheckString(um,ps);
		}
		/// <summary>
		/// 取消按钮
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
			Global.conn.Close();
			Application.Exit();
		}

		private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			//if(e.Button == MouseButtons.Left)
			//{
			//	this.Top = Control.MousePosition.Y - e.Y - SystemInformation.FrameBorderSize.Height - SystemInformation.CaptionHeight;
			//	this.Left = Control.MousePosition.X -e.X - SystemInformation.FrameBorderSize.Width;
			//}
		}
	}
}

⌨️ 快捷键说明

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