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

📄 login.cs

📁 visualc#+sql图书出租管理系统
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;

namespace Library
{
	/// <summary>
	/// Login 的摘要说明。
	/// </summary>
	public class LoginForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.TextBox textUserID;
		private System.Windows.Forms.TextBox textUserPassword;
		private System.Windows.Forms.Button btnOk;
		private System.Data.SqlClient.SqlConnection sqlConnection1;
		private System.Windows.Forms.Label lblUser;
		private System.Windows.Forms.Label lblPassword;
		private System.Windows.Forms.Label lbltitle;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

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

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <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.lblUser = new System.Windows.Forms.Label();
			this.lblPassword = new System.Windows.Forms.Label();
			this.btnCancel = new System.Windows.Forms.Button();
			this.textUserID = new System.Windows.Forms.TextBox();
			this.textUserPassword = new System.Windows.Forms.TextBox();
			this.btnOk = new System.Windows.Forms.Button();
			this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
			this.lbltitle = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// lblUser
			// 
			this.lblUser.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.lblUser.Location = new System.Drawing.Point(40, 56);
			this.lblUser.Name = "lblUser";
			this.lblUser.Size = new System.Drawing.Size(72, 23);
			this.lblUser.TabIndex = 1;
			this.lblUser.Text = "用户名:";
			// 
			// lblPassword
			// 
			this.lblPassword.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.lblPassword.Location = new System.Drawing.Point(40, 104);
			this.lblPassword.Name = "lblPassword";
			this.lblPassword.Size = new System.Drawing.Size(72, 23);
			this.lblPassword.TabIndex = 2;
			this.lblPassword.Text = "密  码:";
			// 
			// btnCancel
			// 
			this.btnCancel.Location = new System.Drawing.Point(144, 152);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(64, 24);
			this.btnCancel.TabIndex = 3;
			this.btnCancel.Text = "重置";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// textUserID
			// 
			this.textUserID.Location = new System.Drawing.Point(112, 56);
			this.textUserID.Name = "textUserID";
			this.textUserID.Size = new System.Drawing.Size(96, 21);
			this.textUserID.TabIndex = 0;
			this.textUserID.Text = "";
			// 
			// textUserPassword
			// 
			this.textUserPassword.Location = new System.Drawing.Point(112, 104);
			this.textUserPassword.Name = "textUserPassword";
			this.textUserPassword.PasswordChar = '*';
			this.textUserPassword.Size = new System.Drawing.Size(96, 21);
			this.textUserPassword.TabIndex = 1;
			this.textUserPassword.Text = "";
			// 
			// btnOk
			// 
			this.btnOk.Location = new System.Drawing.Point(40, 152);
			this.btnOk.Name = "btnOk";
			this.btnOk.Size = new System.Drawing.Size(64, 24);
			this.btnOk.TabIndex = 6;
			this.btnOk.Text = "登录";
			this.btnOk.Click += new System.EventHandler(this.btnOk_Click);
			// 
			// sqlConnection1
			// 
			this.sqlConnection1.ConnectionString = "workstation id=WJD;packet size=4096;integrated security=SSPI;data source=WJD;pers" +
				"ist security info=False;initial catalog=BookManagement";
			// 
			// lbltitle
			// 
			this.lbltitle.Font = new System.Drawing.Font("隶书", 21.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.lbltitle.ForeColor = System.Drawing.Color.Blue;
			this.lbltitle.Location = new System.Drawing.Point(56, 8);
			this.lbltitle.Name = "lbltitle";
			this.lbltitle.Size = new System.Drawing.Size(136, 32);
			this.lbltitle.TabIndex = 8;
			this.lbltitle.Text = "登    录";
			this.lbltitle.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// LoginForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.BackColor = System.Drawing.Color.LightGray;
			this.ClientSize = new System.Drawing.Size(250, 194);
			this.Controls.Add(this.lbltitle);
			this.Controls.Add(this.btnOk);
			this.Controls.Add(this.textUserPassword);
			this.Controls.Add(this.textUserID);
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.lblPassword);
			this.Controls.Add(this.lblUser);
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "LoginForm";
			this.Text = "图书出租管理系统";
			this.Load += new System.EventHandler(this.Login_Load);
			this.ResumeLayout(false);

		}
		#endregion

		static void Main() 
		{
			Application.Run(new LoginForm());
		}
		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.textUserID.Text="";
			this.textUserPassword.Text="";
		}

		private void btnOk_Click(object sender, System.EventArgs e)
		{	
			if(UserCheck(this.textUserID.Text,this.textUserPassword.Text)=="system")
			{
				
				this.Visible=false;
				Form mainform=new MainForm(this.textUserID.Text,"system");
				mainform.ShowDialog();
				this.Close();
			}
			else if(UserCheck(this.textUserID.Text,this.textUserPassword.Text)=="user")
			{
				this.Visible=false;
				Form mainform=new MainForm(this.textUserID.Text,"user");
				mainform.ShowDialog();				
				this.Close();
				
			}
			else
			{
				if(MessageBox.Show("输入用户密码有误,是否重新登陆","输入有误",MessageBoxButtons.OKCancel,MessageBoxIcon.Question)==DialogResult.OK)
				{
					this.textUserID.Clear();
					this.textUserPassword.Clear();
					//this.textUserID.();
				}
				else
				{
					this.Close();
				}
			}
		}
		
		string UserCheck(string username,string userpassword)
		{
			string txtSql,usersort;	
			usersort="nobody";
			txtSql = "SELECT * FROM [User]";
			SqlCommand checkuser=new SqlCommand(txtSql,this.sqlConnection1);
			try
			{
				this.sqlConnection1.Open();
				SqlDataReader sqlreader=checkuser.ExecuteReader();
				while(sqlreader.Read())
				{	
					if((sqlreader[0].ToString().Trim()==username)&&(sqlreader[1].ToString().Trim()==userpassword))
					{						
						usersort=sqlreader[2].ToString().Trim();
						break;						
					}
				}		
			}
			catch(Exception e)
			{
				//Console.WriteLine(e.ToString());
				MessageBox.Show(e.ToString());
			}			
			sqlConnection1.Close();	
			//return "nobody";	
			return usersort;
		}

		private void Login_Load(object sender, System.EventArgs e)
		{
			//this.sqlConnection1.Open();
			//this.sqlDataAdapter1.Fill(this.dataSetUser1);
		}

	


	}
}

⌨️ 快捷键说明

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