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

📄 login.cs

📁 c#+SQL Server 中小型信息系统开发实例精选书上代码
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace StudentManagementsystem
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Login : System.Windows.Forms.Form
	{
		
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button login_butt;
		private System.Windows.Forms.Button Logout_butt;
		private System.Windows.Forms.TextBox User_name;
		private System.Windows.Forms.TextBox Passwords;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		/// 系统运行所需要的变量。
		/// 
		
		public static String Username="";//保存用户登陆名,以便以后使用。
		public static bool loginOrnot;//判断用户登陆成功与否。
		private string strconn;//查询更新数据库字符串。
		private string connstr="workstation id=localhost;Integrated Security=SSPI;database=StuMagSys";//连接数据库字符串。
		private SqlDataAdapter sqlda;//查询数据库时使用的适配器。
        private DataSet dset =new DataSet();//保存查询结果的数据集。
 		private SqlConnection myconn;//数据库连接。
		private DataTable mytable;//查询返回的数据表。
		private DataRow Userrows;//表中的行。
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.ComponentModel.Container components = null;

		public Login()
		{
			//
			// 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.login_butt = new System.Windows.Forms.Button();
			this.Logout_butt = new System.Windows.Forms.Button();
			this.User_name = new System.Windows.Forms.TextBox();
			this.Passwords = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.SuspendLayout();
			// 
			// login_butt
			// 
			this.login_butt.Location = new System.Drawing.Point(32, 136);
			this.login_butt.Name = "login_butt";
			this.login_butt.Size = new System.Drawing.Size(80, 24);
			this.login_butt.TabIndex = 1;
			this.login_butt.Text = "登 陆";
			this.login_butt.Click += new System.EventHandler(this.login_butt_Click);
			// 
			// Logout_butt
			// 
			this.Logout_butt.Location = new System.Drawing.Point(144, 136);
			this.Logout_butt.Name = "Logout_butt";
			this.Logout_butt.Size = new System.Drawing.Size(80, 24);
			this.Logout_butt.TabIndex = 2;
			this.Logout_butt.Text = "退 出";
			this.Logout_butt.Click += new System.EventHandler(this.Logout_butt_Click);
			// 
			// User_name
			// 
			this.User_name.Location = new System.Drawing.Point(112, 40);
			this.User_name.Name = "User_name";
			this.User_name.Size = new System.Drawing.Size(104, 21);
			this.User_name.TabIndex = 3;
			this.User_name.Text = "";
			// 
			// Passwords
			// 
			this.Passwords.Location = new System.Drawing.Point(112, 80);
			this.Passwords.Name = "Passwords";
			this.Passwords.PasswordChar = '*';
			this.Passwords.Size = new System.Drawing.Size(104, 21);
			this.Passwords.TabIndex = 4;
			this.Passwords.Text = "";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(40, 48);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(56, 16);
			this.label1.TabIndex = 5;
			this.label1.Text = "用户名:";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(40, 88);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(56, 16);
			this.label2.TabIndex = 6;
			this.label2.Text = "密  码:";
			// 
			// groupBox1
			// 
			this.groupBox1.Location = new System.Drawing.Point(24, 16);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(208, 104);
			this.groupBox1.TabIndex = 7;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "请输入用户名密码";
			// 
			// groupBox2
			// 
			this.groupBox2.Location = new System.Drawing.Point(24, 120);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(208, 48);
			this.groupBox2.TabIndex = 8;
			this.groupBox2.TabStop = false;
			// 
			// Login
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(256, 189);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.Passwords);
			this.Controls.Add(this.User_name);
			this.Controls.Add(this.Logout_butt);
			this.Controls.Add(this.login_butt);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.groupBox2);
			this.Name = "Login";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "用户登录";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]

		private void login_butt_Click(object sender, System.EventArgs e)
		{
			try
			{
				strconn="select * from syuser where Use_name="+"'"+this.User_name.Text.ToString().Trim()+"'";//根据用户输入的用户名和密码初始化查询更新数据库字符串
				myconn=new SqlConnection(connstr);
				sqlda=new SqlDataAdapter(strconn,myconn);
				dset.Clear();
				sqlda.Fill(dset,"syuser");
				
				mytable=dset.Tables[0];//查询的结果在Tables[0]中。
				for(int j=0;j<mytable.Rows.Count;j++)
				{
					Userrows=mytable.Rows[j];

					if(Userrows[1].ToString().Trim()==this.User_name.Text.ToString().Trim()&&Userrows[3].ToString().Trim()==this.Passwords.Text.ToString().Trim()) {
						loginOrnot=true;
						Username=Userrows[1].ToString().Trim();
						this.Close();
					}
					else 
					{
						MessageBox.Show("用户名/密码错误!请重试!","确认",MessageBoxButtons.OK);
						loginOrnot=false;
						this.User_name.Text="";
						this.Passwords.Text="";
					}
				}

			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.ToString(),"确认",MessageBoxButtons.YesNoCancel);
			}
		return;
		}

		private void Logout_butt_Click(object sender, System.EventArgs e)
		{
			loginOrnot=false;
			this.Close();
		}
	}
}

⌨️ 快捷键说明

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