📄 frmlogin.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OracleClient;
using System.Net;
namespace GlobleUtility
{
/// <summary>
/// 类名: CfrmLogin
/// 功能: 用户登录
/// 使用方法: 定义变量,然后以有模式方式显示窗体
/// </summary>
public class CfrmLogin : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox edtUserName;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox edtPassword;
private System.Windows.Forms.Button btnLogin;
private System.Windows.Forms.Button btnCancel;
private System.ComponentModel.IContainer components;
private System.Windows.Forms.ToolTip toolTip;
private System.Windows.Forms.PictureBox picDBSetting; //登录用户所使用的数据库用户名
public CfrmLogin()
{
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.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(CfrmLogin));
this.label1 = new System.Windows.Forms.Label();
this.edtUserName = new System.Windows.Forms.TextBox();
this.edtPassword = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.btnLogin = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.picDBSetting = new System.Windows.Forms.PictureBox();
this.toolTip = new System.Windows.Forms.ToolTip(this.components);
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(48, 16);
this.label1.TabIndex = 0;
this.label1.Text = "用户名";
//
// edtUserName
//
this.edtUserName.Location = new System.Drawing.Point(72, 16);
this.edtUserName.Name = "edtUserName";
this.edtUserName.Size = new System.Drawing.Size(152, 21);
this.edtUserName.TabIndex = 1;
this.edtUserName.Text = "";
this.edtUserName.TextChanged += new System.EventHandler(this.edtUserName_TextChanged);
//
// edtPassword
//
this.edtPassword.Location = new System.Drawing.Point(72, 48);
this.edtPassword.Name = "edtPassword";
this.edtPassword.PasswordChar = '*';
this.edtPassword.Size = new System.Drawing.Size(152, 21);
this.edtPassword.TabIndex = 3;
this.edtPassword.Text = "";
this.edtPassword.TextChanged += new System.EventHandler(this.edtPassword_TextChanged);
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 48);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(48, 16);
this.label2.TabIndex = 2;
this.label2.Text = "密码";
//
// btnLogin
//
this.btnLogin.Enabled = false;
this.btnLogin.Location = new System.Drawing.Point(72, 88);
this.btnLogin.Name = "btnLogin";
this.btnLogin.Size = new System.Drawing.Size(72, 24);
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.Location = new System.Drawing.Point(152, 88);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(72, 24);
this.btnCancel.TabIndex = 5;
this.btnCancel.Text = "取消(&C)";
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// picDBSetting
//
this.picDBSetting.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.picDBSetting.Image = ((System.Drawing.Image)(resources.GetObject("picDBSetting.Image")));
this.picDBSetting.Location = new System.Drawing.Point(16, 88);
this.picDBSetting.Name = "picDBSetting";
this.picDBSetting.Size = new System.Drawing.Size(24, 24);
this.picDBSetting.TabIndex = 0;
this.picDBSetting.TabStop = false;
this.picDBSetting.Click += new System.EventHandler(this.picDBSetting_Click);
this.picDBSetting.MouseEnter += new System.EventHandler(this.picDBSetting_MouseEnter);
//
// CfrmLogin
//
this.AcceptButton = this.btnLogin;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.CancelButton = this.btnCancel;
this.ClientSize = new System.Drawing.Size(240, 127);
this.Controls.Add(this.picDBSetting);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnLogin);
this.Controls.Add(this.edtPassword);
this.Controls.Add(this.edtUserName);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Cursor = System.Windows.Forms.Cursors.Default;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "CfrmLogin";
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "用户登录";
this.Load += new System.EventHandler(this.CfrmLogin_Load);
this.MouseEnter += new System.EventHandler(this.CfrmLogin_MouseEnter);
this.ResumeLayout(false);
}
#endregion
private void edtUserName_TextChanged(object sender, System.EventArgs e)
{
if(ValidateParam())
btnLogin.Enabled = true;
else
btnLogin.Enabled = false;
}
private void edtPassword_TextChanged(object sender, System.EventArgs e)
{
if(ValidateParam())
btnLogin.Enabled = true;
else
btnLogin.Enabled = false;
}
private bool ValidateParam()
{
//检查数据的合法性。注意,这里只是简单地检查是否有字符输入。
if(edtUserName.Text.Trim().Length == 0)
return false;
if(edtPassword.Text.Trim().Length == 0)
return false;
return true;
}
private void btnLogin_Click(object sender, System.EventArgs e)
{
this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
if(Login(this.edtUserName.Text.Trim(), this.edtPassword.Text.Trim()) != true)
return;
this.DialogResult = DialogResult.OK;
this.Cursor = System.Windows.Forms.Cursors.Default;
Close();
}
private void btnCancel_Click(object sender, System.EventArgs e)
{
if(Connection.DBConnection != null)
{
if(Connection.DBConnection.State != ConnectionState.Closed)
Connection.DBConnection.Close();
}
Close();
}
private void CfrmLogin_Load(object sender, System.EventArgs e)
{
toolTip.SetToolTip(picDBSetting, "设置数据库");
}
private void picDBSetting_MouseEnter(object sender, System.EventArgs e)
{
picDBSetting.BorderStyle = BorderStyle.Fixed3D;
}
private void CfrmLogin_MouseEnter(object sender, System.EventArgs e)
{
picDBSetting.BorderStyle = BorderStyle.FixedSingle;
}
private void picDBSetting_Click(object sender, System.EventArgs e)
{
}
public bool Login(string sLoginDBUser, string sPassword)
{
this.edtUserName.Text = sLoginDBUser;
this.edtPassword.Text = sPassword;
Connection Conn = new Connection();
int nResult = Conn.Open();
if(nResult > 0)
{
this.Cursor = System.Windows.Forms.Cursors.Default;
ErrorCode E = new ErrorCode();
E.ShowMessage(nResult);
return false;
}
string checkuser="select t1.userid,t2.wsid,t1.right "+
"from charge_Admin_Info t1,charge_WS_Info t2 "+
"where t1.UserName='"+this.edtUserName.Text.Trim()+"' and "+
"t1.PassWord='"+this.edtPassword.Text.Trim()+"' and "+
"t2.IPAddr=";
IPHostEntry hostInfo = Dns.GetHostByName(SystemInformation.ComputerName);
IPAddress[] address = hostInfo.AddressList;
OracleCommand Cmd = new OracleCommand();
Cmd.Connection = Connection.DBConnection;
Cmd.CommandType = CommandType.Text;
OracleDataReader Reader;
bool bValidUser = false;
for(int index = 0; index < address.Length; index++)
{
Cmd.CommandText = checkuser + "'" + address[index] + "'";
Reader = Cmd.ExecuteReader();
Reader.Read();
if(Reader.HasRows)
{
Connection.WSID = Reader.GetInt32(1);
Connection.RightID = Reader.GetInt32(2);
bValidUser = true;
break;
}
Reader.Close();
}
Cmd.Dispose();
if(!bValidUser)
{
if(Connection.DBConnection != null)
{
if(Connection.DBConnection.State != ConnectionState.Closed)
Connection.DBConnection.Close();
}
this.Cursor = System.Windows.Forms.Cursors.Default;
ErrorCode E = new ErrorCode();
E.ShowMessage(ErrorCode.E_4_VerifyUserFailed_Code);
return false;
}
//用户验证成功,此时记录下用户名
Connection.LoginUser = this.edtUserName.Text.Trim();
return true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -