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

📄 logon.cs

📁 这是用SQL数据库做的学生信息管理系统。简明
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace 学生与成绩
{
	/// <summary>
	/// Logon 的摘要说明。
	/// </summary>
	public class Logon : System.Windows.Forms.Form
	{
		private DataSet ds=new DataSet();
		private string sendStrSQL="select * from 用户";
		private string sendTableName;
		private DataTable myTable;
		private DataRow myRow;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox txtName;
		private System.Windows.Forms.TextBox txtNewName;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.TextBox txtNewPswd;
		private System.Windows.Forms.TextBox txtConfirmP;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.TextBox txtPswd;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Button btnConfirm;
		private System.Windows.Forms.Button btnQuit;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Logon()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();
			LinkDataBase link=new LinkDataBase();
			sendTableName="用户";
			this.ds=link.SelectDataBase(sendStrSQL,sendTableName);
			myTable=ds.Tables[0];

			//
			// 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.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.txtName = new System.Windows.Forms.TextBox();
			this.txtNewName = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.txtNewPswd = new System.Windows.Forms.TextBox();
			this.txtConfirmP = new System.Windows.Forms.TextBox();
			this.label4 = new System.Windows.Forms.Label();
			this.txtPswd = new System.Windows.Forms.TextBox();
			this.label5 = new System.Windows.Forms.Label();
			this.btnConfirm = new System.Windows.Forms.Button();
			this.btnQuit = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(24, 8);
			this.label1.Name = "label1";
			this.label1.TabIndex = 0;
			this.label1.Text = "用户名";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(169, 8);
			this.label2.Name = "label2";
			this.label2.TabIndex = 1;
			this.label2.Text = "新用户名";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// txtName
			// 
			this.txtName.Location = new System.Drawing.Point(24, 40);
			this.txtName.Name = "txtName";
			this.txtName.TabIndex = 0;
			this.txtName.Text = "";
			// 
			// txtNewName
			// 
			this.txtNewName.Location = new System.Drawing.Point(169, 40);
			this.txtNewName.Name = "txtNewName";
			this.txtNewName.TabIndex = 2;
			this.txtNewName.Text = "";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(169, 80);
			this.label3.Name = "label3";
			this.label3.TabIndex = 4;
			this.label3.Text = "新密码";
			this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// txtNewPswd
			// 
			this.txtNewPswd.Location = new System.Drawing.Point(169, 112);
			this.txtNewPswd.Name = "txtNewPswd";
			this.txtNewPswd.PasswordChar = '*';
			this.txtNewPswd.TabIndex = 3;
			this.txtNewPswd.Text = "";
			// 
			// txtConfirmP
			// 
			this.txtConfirmP.Location = new System.Drawing.Point(169, 184);
			this.txtConfirmP.Name = "txtConfirmP";
			this.txtConfirmP.PasswordChar = '*';
			this.txtConfirmP.TabIndex = 4;
			this.txtConfirmP.Text = "";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(169, 152);
			this.label4.Name = "label4";
			this.label4.TabIndex = 6;
			this.label4.Text = "验证密码";
			this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// txtPswd
			// 
			this.txtPswd.Location = new System.Drawing.Point(24, 136);
			this.txtPswd.Name = "txtPswd";
			this.txtPswd.PasswordChar = '*';
			this.txtPswd.TabIndex = 1;
			this.txtPswd.Text = "";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(24, 104);
			this.label5.Name = "label5";
			this.label5.TabIndex = 8;
			this.label5.Text = "密码";
			this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// btnConfirm
			// 
			this.btnConfirm.Location = new System.Drawing.Point(49, 224);
			this.btnConfirm.Name = "btnConfirm";
			this.btnConfirm.TabIndex = 10;
			this.btnConfirm.Text = "确定";
			this.btnConfirm.Click += new System.EventHandler(this.btnConfirm_Click);
			// 
			// btnQuit
			// 
			this.btnQuit.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnQuit.Location = new System.Drawing.Point(169, 224);
			this.btnQuit.Name = "btnQuit";
			this.btnQuit.TabIndex = 11;
			this.btnQuit.Text = "取消";
			this.btnQuit.Click += new System.EventHandler(this.btnQuit_Click);
			// 
			// Logon
			// 
			this.AcceptButton = this.btnConfirm;
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.CancelButton = this.btnQuit;
			this.ClientSize = new System.Drawing.Size(292, 266);
			this.Controls.Add(this.btnQuit);
			this.Controls.Add(this.btnConfirm);
			this.Controls.Add(this.txtPswd);
			this.Controls.Add(this.label5);
			this.Controls.Add(this.txtConfirmP);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.txtNewPswd);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.txtNewName);
			this.Controls.Add(this.txtName);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.Name = "Logon";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "修改用户名或密码";
			this.ResumeLayout(false);

		}
		#endregion

		private void btnConfirm_Click(object sender, System.EventArgs e)
		{
			for (int i=0;i<myTable.Rows.Count;i++)
			{
				this.myRow = myTable.Rows[i];
				//只有当输入的用户名和密码同时对应上数据库中记录时,才能通过校验
				if (myRow[2].ToString().Trim()==this.txtPswd.Text.ToString().Trim()&&myRow[1].ToString().Trim()==this.txtName.Text.Trim())
				{
					string strConn="workstation id=localhost;Integrated Security=SSPI;database=stu01";
					SqlConnection conn=new SqlConnection(strConn);
					conn.Open();
					SqlCommand cmd=conn.CreateCommand();
					string strUserID=myRow[0].ToString().Trim();//关键代码,决定是否能够修改资料成功

					if(this.txtConfirmP.Text==""&&this.txtNewPswd.Text==""&&this.txtNewName.Text.Trim()=="")
					{
						MessageBox.Show("没有修改用户信息","信息");
						this.Close();
						return;
					}

					if(this.txtConfirmP.Text.Trim()==this.txtNewPswd.Text.Trim())
					{
						if(this.txtName.Text=="")
						{
							cmd.CommandText="update 用户 set 密码='"+this.txtNewPswd.Text.Trim()
								+"' where 编号='"+strUserID.Trim()+"'";
							cmd.ExecuteNonQuery();
							conn.Close();
							MessageBox.Show( "用户信息修改成功,以后请使用新的登录信息.","恭喜您",MessageBoxButtons.OK,MessageBoxIcon.Information);
							this.Close();
							return;
						}
						if(this.txtNewPswd.Text!=null)
						{
							cmd.CommandText="update 用户 set 密码='"+this.txtNewPswd.Text.Trim()
								+"',用户名='"+this.txtName.Text.Trim()+"' where 编号='"+strUserID.Trim()+"'";
							cmd.ExecuteNonQuery();
							conn.Close();
							MessageBox.Show( "用户信息修改成功,以后请使用新的登录信息.","恭喜您",MessageBoxButtons.OK,MessageBoxIcon.Information);
							//link.UpdateDataBase(ds,"用户");
							this.Close();
							return;
						}
					}
					else
					{
						MessageBox.Show("您两次输入的新密码不相同,请核对后重新输入.","密码修改不成功",MessageBoxButtons.OK,MessageBoxIcon.Warning);
						this.txtNewPswd.Clear();
						this.txtNewPswd.Focus();
						this.txtConfirmP.Clear();
						return;
					}
					
					
				}
			}
			for (int i=0;i<myTable.Rows.Count;i++)
			{
				this.myRow = myTable.Rows[i];
				if(myRow[2].ToString().Trim()!=this.txtPswd.Text.ToString().Trim()||myRow[1].ToString().Trim()!=this.txtName.Text.Trim())
				{
					MessageBox.Show( "您输入的用户名或密码不正确!","修改不成功",MessageBoxButtons.OK,MessageBoxIcon.Warning);
					this.txtPswd.Clear();
					this.txtName.Clear();
					this.txtName.Focus();
					return;
				}
			}
			MessageBox.Show( "您输入信息不完整","修改不成功",MessageBoxButtons.OK,MessageBoxIcon.Warning);
			return;
		}

		private void btnQuit_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}
	}
}

⌨️ 快捷键说明

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