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

📄 logform.cs

📁 一个经典的通讯录管理程序源代码
💻 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>
	/// Summary description for Form1.
	/// </summary>
	public class LogForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button buttonExit;
		private System.Windows.Forms.Button buttonLogOn;
		private System.Windows.Forms.Button buttonNewUser;
		private System.Windows.Forms.TextBox textBoxPW;
		private System.Windows.Forms.TextBox textBoxUser;
		private System.Windows.Forms.Label labelPW;
		private System.Windows.Forms.Label labelUser;
		private System.ComponentModel.IContainer components;
		private MainForm mainForm;

		public LogForm(MainForm pForm)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//

			// 记住父窗口指针
			mainForm = pForm;
            
			// 根据myConnection的状态返回信息
		    if (mainForm.myConnection.State == System.Data.ConnectionState.Closed)
			{
				// 弹出对话框提示登录失败
				MessageBox.Show("出错,不能连接服务器!");

				// 改变LogForm的Text表示登录失败
				this.Text = "登录数据库失败,请退出检查与数据库的连接";

				// 禁止新建用户和登录功能
				buttonNewUser.Enabled = false;
				buttonLogOn.Enabled = false;
			}
			
		}


		public LogForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//

			// 根据myConnection的状态返回信息
			if (mainForm.myConnection.State == System.Data.ConnectionState.Closed)
			{
				// 弹出对话框提示登录失败
				MessageBox.Show("出错,不能连接服务器!");

				// 改变LogForm的Text表示登录失败
				this.Text = "登录数据库失败,请退出检查与数据库的连接";

				// 禁止新建用户和登录功能
				buttonNewUser.Enabled = false;
				buttonLogOn.Enabled = false;
			}
			
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.buttonExit = new System.Windows.Forms.Button();
			this.buttonLogOn = new System.Windows.Forms.Button();
			this.buttonNewUser = new System.Windows.Forms.Button();
			this.textBoxPW = new System.Windows.Forms.TextBox();
			this.textBoxUser = new System.Windows.Forms.TextBox();
			this.labelPW = new System.Windows.Forms.Label();
			this.labelUser = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// buttonExit
			// 
			this.buttonExit.Location = new System.Drawing.Point(224, 72);
			this.buttonExit.Name = "buttonExit";
			this.buttonExit.Size = new System.Drawing.Size(72, 24);
			this.buttonExit.TabIndex = 13;
			this.buttonExit.Text = "退出";
			this.buttonExit.Click += new System.EventHandler(this.buttonExit_Click);
			// 
			// buttonLogOn
			// 
			this.buttonLogOn.Location = new System.Drawing.Point(224, 40);
			this.buttonLogOn.Name = "buttonLogOn";
			this.buttonLogOn.Size = new System.Drawing.Size(72, 24);
			this.buttonLogOn.TabIndex = 12;
			this.buttonLogOn.Text = "登录";
			this.buttonLogOn.Click += new System.EventHandler(this.buttonLogOn_Click);
			// 
			// buttonNewUser
			// 
			this.buttonNewUser.Location = new System.Drawing.Point(224, 8);
			this.buttonNewUser.Name = "buttonNewUser";
			this.buttonNewUser.Size = new System.Drawing.Size(72, 24);
			this.buttonNewUser.TabIndex = 11;
			this.buttonNewUser.Text = "新用户";
			this.buttonNewUser.Click += new System.EventHandler(this.buttonNewUser_Click);
			// 
			// textBoxPW
			// 
			this.textBoxPW.Location = new System.Drawing.Point(80, 56);
			this.textBoxPW.Name = "textBoxPW";
			this.textBoxPW.PasswordChar = '*';
			this.textBoxPW.Size = new System.Drawing.Size(120, 21);
			this.textBoxPW.TabIndex = 10;
			this.textBoxPW.Text = "";
			// 
			// textBoxUser
			// 
			this.textBoxUser.Location = new System.Drawing.Point(80, 16);
			this.textBoxUser.Name = "textBoxUser";
			this.textBoxUser.Size = new System.Drawing.Size(120, 21);
			this.textBoxUser.TabIndex = 9;
			this.textBoxUser.Text = "";
			// 
			// labelPW
			// 
			this.labelPW.Location = new System.Drawing.Point(16, 56);
			this.labelPW.Name = "labelPW";
			this.labelPW.Size = new System.Drawing.Size(64, 23);
			this.labelPW.TabIndex = 8;
			this.labelPW.Text = "密码";
			this.labelPW.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// labelUser
			// 
			this.labelUser.Location = new System.Drawing.Point(16, 16);
			this.labelUser.Name = "labelUser";
			this.labelUser.Size = new System.Drawing.Size(64, 23);
			this.labelUser.TabIndex = 7;
			this.labelUser.Text = "用户名";
			this.labelUser.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// LogForm
			// 
			this.AcceptButton = this.buttonLogOn;
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(320, 109);
			this.ControlBox = false;
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.buttonExit,
																		  this.buttonLogOn,
																		  this.buttonNewUser,
																		  this.textBoxPW,
																		  this.textBoxUser,
																		  this.labelPW,
																		  this.labelUser});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "LogForm";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "登录";
			this.Load += new System.EventHandler(this.LogForm_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new MainForm());
		}

		private void LogForm_Load(object sender, System.EventArgs e)
		{

		
		}

		private void buttonLogOn_Click(object sender, System.EventArgs e)
		{
            // 设置SQL命令字符串
			string commandstring;
			commandstring = "SELECT * FROM Users";

            // 使用SqlCommand  
			SqlCommand myCommand;
			myCommand = new SqlCommand();
			myCommand.Connection = mainForm.myConnection;
			myCommand.CommandText = commandstring;

            // 执行ExecuteReader()返回DataReader类
			SqlDataReader myDataReader;
			myDataReader = myCommand.ExecuteReader();
			bool b_accept = false;

			// 循环读取DataReader中的每一个记录,看用户密码是否正确
			while (myDataReader.Read())
			{
              // 如果用户名和密码正确的话置b_accept为真
				if (myDataReader.GetString(0).Trim() ==textBoxUser.Text.Trim()  && 
					myDataReader.GetString(1).Trim()==textBoxPW.Text.Trim())
				{
					b_accept = true;
					break;
				}
			};

			myDataReader.Close();

			// 如果用户密码正确,关闭当前窗口,否则显示错误信息
			if (b_accept==true) 
			{
				mainForm.currentUser = textBoxUser.Text;
				this.Close();
			}
			else 
			{
				MessageBox.Show("用户名或密码不正确,请重新输入!","错误");
			}
		}


		private void buttonNewUser_Click(object sender, System.EventArgs e)
		{
			DialogResult result;

			// 弹出确认信息
			result = MessageBox.Show(this,"确定要添加新用户吗?", "提示", MessageBoxButtons.YesNo,
				MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, 
				MessageBoxOptions.RightAlign);

			// 确认要添加
			if(result == DialogResult.Yes)
			{
				string commandstring;
				commandstring = "SELECT * FROM Users";

				// 使用SqlCommand  
				SqlCommand myCommand;
				myCommand = new SqlCommand();
				myCommand.Connection = mainForm.myConnection;
				myCommand.CommandText = commandstring;

				// 执行ExecuteReader()返回DataReader类
				SqlDataReader myDataReader;
				myDataReader = myCommand.ExecuteReader();
				bool b_exist = false;

				// 循环读取DataReader中的每一个记录,看用户是否已经存在
				while (myDataReader.Read())
				{
					// 如果用户名已经存在的话设置b_exist为true
					if (myDataReader.GetString(0).Trim() ==textBoxUser.Text.Trim() )
					{
						b_exist = true;
						break;
					}
				};

				myDataReader.Close();           

                // 弹出提示信息,并退出新建用户事件
				if (b_exist) 
				{
					MessageBox.Show("对不起,该用户名已存在,请重新输入!","错误");
					return;
				}

				// 用户不存在,添加用户
				string sInsert = "INSERT INTO Users (用户名, 密码) " +
                     "VALUES ('" + textBoxUser.Text.Trim() + "','" + textBoxPW.Text.Trim() + "')";
                
				// 设置添加记录字符串
				myCommand.CommandText = sInsert;
				myCommand.Connection = mainForm.myConnection;
				
				// 执行SQL的管理命令
				myCommand.ExecuteNonQuery();
                
				// 设置MainForm的当前用户,关闭登录窗口
				mainForm.currentUser = textBoxUser.Text.Trim();
				this.Close();
			}
		}

		private void buttonExit_Click(object sender, System.EventArgs e)
		{
			Application.Exit();
		}

	}
}

⌨️ 快捷键说明

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