📄 frmlogin.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Win32;
namespace GEROSYS
{
/// <summary>
/// frmLogin 的摘要说明。
/// </summary>
public class frmLogin : System.Windows.Forms.Form
{
private System.Windows.Forms.Button cmdAccept;
private System.Windows.Forms.Button cmdCancel;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtUserId;
private System.Windows.Forms.TextBox txtPassword;
private System.Windows.Forms.Label label2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private DataSet ldsUser;
private DataTable ldtUser;
private System.ComponentModel.Container components = null;
private string strSQL;
public string gUserId;
public string gUserName;
public GlobalPara g=new GlobalPara();
private string Connectionstring = new AccessData().ConnectionString;
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.cmdAccept = new System.Windows.Forms.Button();
this.cmdCancel = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.label2 = new System.Windows.Forms.Label();
this.txtPassword = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.txtUserId = new System.Windows.Forms.TextBox();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// cmdAccept
//
this.cmdAccept.Location = new System.Drawing.Point(48, 136);
this.cmdAccept.Name = "cmdAccept";
this.cmdAccept.Size = new System.Drawing.Size(72, 24);
this.cmdAccept.TabIndex = 0;
this.cmdAccept.Text = "确认";
this.cmdAccept.Click += new System.EventHandler(this.cmdAccept_Click);
//
// cmdCancel
//
this.cmdCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.cmdCancel.Location = new System.Drawing.Point(184, 136);
this.cmdCancel.Name = "cmdCancel";
this.cmdCancel.Size = new System.Drawing.Size(72, 24);
this.cmdCancel.TabIndex = 1;
this.cmdCancel.Text = "取消";
this.cmdCancel.Click += new System.EventHandler(this.cmdCancel_Click);
//
// groupBox1
//
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.txtPassword);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Controls.Add(this.txtUserId);
this.groupBox1.Location = new System.Drawing.Point(40, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(224, 112);
this.groupBox1.TabIndex = 2;
this.groupBox1.TabStop = false;
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 72);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(48, 16);
this.label2.TabIndex = 4;
this.label2.Text = "密码:";
//
// txtPassword
//
this.txtPassword.AcceptsReturn = true;
this.txtPassword.Location = new System.Drawing.Point(80, 72);
this.txtPassword.Name = "txtPassword";
this.txtPassword.PasswordChar = '*';
this.txtPassword.Size = new System.Drawing.Size(120, 21);
this.txtPassword.TabIndex = 1;
this.txtPassword.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 16);
this.label1.TabIndex = 0;
this.label1.Text = "工号:";
//
// txtUserId
//
this.txtUserId.Location = new System.Drawing.Point(80, 32);
this.txtUserId.Name = "txtUserId";
this.txtUserId.Size = new System.Drawing.Size(120, 21);
this.txtUserId.TabIndex = 3;
this.txtUserId.Text = "";
//
// frmLogin
//
this.AcceptButton = this.cmdAccept;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.CancelButton = this.cmdCancel;
this.ClientSize = new System.Drawing.Size(304, 186);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.cmdCancel);
this.Controls.Add(this.cmdAccept);
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.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
// [STAThread]
// static void Main()
// {
// Application.Run(new frmSplash());
// }
private void cmdAccept_Click(object sender, System.EventArgs e)
{
SqlConnection cnSQL;
SqlCommand cmSQL;
SqlDataReader drSQL;
RegistryKey regRead;
RegistryKey regWrite;
try
{
strSQL = "SELECT user_id, user_name, user_cnname,password,role_id FROM USERS where user_name='" + txtUserId.Text.Trim().ToString() + "'";
cnSQL = new SqlConnection(Connectionstring);
cnSQL.Open();
cmSQL = new SqlCommand(strSQL, cnSQL);
drSQL = cmSQL.ExecuteReader();
if (drSQL.Read())
{
if (drSQL["password"].ToString() != txtPassword.Text.Trim())
{
MessageBox.Show ( "用户密码不对,请重新输入!","提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txtPassword.Focus();
SendKeys.Send( "{Home}+{End}");
return;
}
regRead= Registry.CurrentUser.OpenSubKey("Software\\GERO",true);
if(regRead==null)
{
regWrite = Registry.CurrentUser.CreateSubKey("Software\\GERO");
regWrite.SetValue("COM",1);
regWrite.Close();
}
else
{
object obj= regRead.GetValue("COM");
GlobalPara.ComValue =Int32.Parse(obj.ToString());
regRead.Close();
}
g.gUserId = drSQL["user_name"].ToString() ;
g.gUserName =drSQL["user_cnname"].ToString() ;
g.gRoleId=drSQL["role_id"].ToString() ;
this.Hide();
GEROMain frm=new GEROMain(this);
frm.Show();
}
else
{
MessageBox.Show( "用户名输入有误,请重新输入!","提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
this.txtUserId.Focus();
SendKeys.Send ( "{Home}+{End}");
return;
}
drSQL.Close();
cnSQL.Close();
cmSQL.Dispose();
cnSQL.Dispose();
}
catch(SqlException ms)
{
MessageBox.Show(ms.Message, "SQL Error");
}
catch(Exception ms)
{
MessageBox.Show(ms.Message, "General Error");
}
}
private void frmLogin_Load(object sender, System.EventArgs e)
{
this.txtUserId.Focus();
}
private void cmdCancel_Click(object sender, System.EventArgs e)
{
if (MessageBox.Show("你确实要退出系统吗?","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Exclamation) == DialogResult.Yes)
{
Application.Exit();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -