📄 frmlogin.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace HotelApp
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class frmLogin : System.Windows.Forms.Form
{
protected DataSet dsLogin;
protected DBService_2 dbcon;
private System.Windows.Forms.Label LabelUser;
private System.Windows.Forms.Label LabelPwd;
private System.Windows.Forms.TextBox txtUser;
private System.Windows.Forms.TextBox txtPwd;
private System.Windows.Forms.Button btnLogin;
private System.Windows.Forms.Button btnCancel;
private int cnt;
private DataRow row;
private string tempU;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public frmLogin()
{
//
// 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.LabelUser = new System.Windows.Forms.Label();
this.LabelPwd = new System.Windows.Forms.Label();
this.txtUser = new System.Windows.Forms.TextBox();
this.txtPwd = new System.Windows.Forms.TextBox();
this.btnLogin = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// LabelUser
//
this.LabelUser.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.LabelUser.Location = new System.Drawing.Point(40, 28);
this.LabelUser.Name = "LabelUser";
this.LabelUser.Size = new System.Drawing.Size(54, 15);
this.LabelUser.TabIndex = 0;
this.LabelUser.Text = "用户名:";
//
// LabelPwd
//
this.LabelPwd.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.LabelPwd.Location = new System.Drawing.Point(40, 67);
this.LabelPwd.Name = "LabelPwd";
this.LabelPwd.Size = new System.Drawing.Size(54, 15);
this.LabelPwd.TabIndex = 1;
this.LabelPwd.Text = "密码:";
//
// txtUser
//
this.txtUser.Location = new System.Drawing.Point(96, 25);
this.txtUser.Name = "txtUser";
this.txtUser.Size = new System.Drawing.Size(124, 21);
this.txtUser.TabIndex = 2;
this.txtUser.Text = "";
//
// txtPwd
//
this.txtPwd.Location = new System.Drawing.Point(96, 64);
this.txtPwd.Name = "txtPwd";
this.txtPwd.PasswordChar = '*';
this.txtPwd.Size = new System.Drawing.Size(124, 21);
this.txtPwd.TabIndex = 3;
this.txtPwd.Text = "";
//
// btnLogin
//
this.btnLogin.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btnLogin.Location = new System.Drawing.Point(49, 125);
this.btnLogin.Name = "btnLogin";
this.btnLogin.Size = new System.Drawing.Size(72, 23);
this.btnLogin.TabIndex = 4;
this.btnLogin.Text = "登录(&L)";
this.btnLogin.Click += new System.EventHandler(this.btnLogin_Click);
//
// btnCancel
//
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnCancel.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btnCancel.Location = new System.Drawing.Point(140, 125);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(72, 23);
this.btnCancel.TabIndex = 5;
this.btnCancel.Text = "取消(&C)";
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// frmLogin
//
this.AcceptButton = this.btnLogin;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.CancelButton = this.btnCancel;
this.ClientSize = new System.Drawing.Size(261, 173);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnLogin);
this.Controls.Add(this.txtPwd);
this.Controls.Add(this.txtUser);
this.Controls.Add(this.LabelPwd);
this.Controls.Add(this.LabelUser);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "frmLogin";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "用户登录";
this.Load += new System.EventHandler(this.frmLogin_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmLogin());
}
private void frmLogin_Load(object sender, System.EventArgs e)
{
dbcon = new DBService_2();
}
private void btnLogin_Click(object sender, System.EventArgs e)
{
//检验用户名是否填写
if(txtUser.Text == "")
{
MessageBox.Show(this,"请输入用户名!","用户登录",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
txtUser.Focus();
return;
}
//检验密码是否填写
if(txtPwd.Text == "")
{
MessageBox.Show(this,"请输入密码!","用户登录",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
txtPwd.Focus();
return;
}
string sqlstr = "select hotelID,User,Password from hotel";
//string sqlstr = "select User,Password from hotel where user='"+txtUser.Text.Trim()+"'";
dsLogin = dbcon.executeBySQL(sqlstr);
cnt = dsLogin.Tables[0].Rows.Count; //取得数据集中表的行数。
//检验登录
if(cnt > 0)
{
for(int i = 0; i < cnt; i++)
{
row = dsLogin.Tables[0].Rows[i];
if(row["User"].Equals(txtUser.Text.Trim())) //核对用户名
{
if(row["Password"].Equals(txtPwd.Text.Trim())) //核对密码
{
tempU = txtUser.Text.Trim();
this.Visible=false; //frmLogin窗体隐藏
frmRoomQuery feh = new frmRoomQuery();
GlobalVars.temp = tempU; //将用户名赋值给GlobalVars的属性temp
GlobalVars.temp_ID = row["hotelID"].ToString();
feh.Show();
break; //如果用户名密码正确则跳出循环
//return;
}
else
{
MessageBox.Show(this,"请输入正确密码!","用户登录",MessageBoxButtons.OK,MessageBoxIcon.Error);
txtPwd.Focus();
}
}
else
{
if(i == cnt-1)
{
MessageBox.Show(this,"数据库中不存在此用户名,请重新输入!","用户登录",MessageBoxButtons.OK,MessageBoxIcon.Error);
txtUser.Focus();
}
}
}
}
else
{
MessageBox.Show(this,"错误!表中无记录!","用户登录",MessageBoxButtons.OK,MessageBoxIcon.Error);
txtUser.Focus();
}
}
private void btnCancel_Click(object sender, System.EventArgs e)
{
txtUser.Text = "";
txtPwd.Text = "";
txtUser.Focus();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -