📄 frmlogin.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace OneToMany
{
/// <summary>
/// frmLogin 的摘要说明。
/// </summary>
public class frmLogin : System.Windows.Forms.Form
{
private System.Windows.Forms.ImageList imageList1;
private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.Button btnOK;
private System.Windows.Forms.PictureBox pictureBox2;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtPwd;
private System.Windows.Forms.TextBox txtUser;
private System.Windows.Forms.PictureBox pictureBox3;
private System.ComponentModel.IContainer components;
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.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmLogin));
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.btnCancel = new System.Windows.Forms.Button();
this.btnOK = new System.Windows.Forms.Button();
this.pictureBox2 = new System.Windows.Forms.PictureBox();
this.txtPwd = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.label1 = new System.Windows.Forms.Label();
this.txtUser = new System.Windows.Forms.TextBox();
this.pictureBox3 = new System.Windows.Forms.PictureBox();
this.SuspendLayout();
//
// imageList1
//
this.imageList1.ImageSize = new System.Drawing.Size(32, 32);
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
//
// btnCancel
//
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnCancel.Image = ((System.Drawing.Image)(resources.GetObject("btnCancel.Image")));
this.btnCancel.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.btnCancel.Location = new System.Drawing.Point(216, 176);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(80, 32);
this.btnCancel.TabIndex = 4;
this.btnCancel.Text = "取消(&C)";
this.btnCancel.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// btnOK
//
this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
this.btnOK.Image = ((System.Drawing.Image)(resources.GetObject("btnOK.Image")));
this.btnOK.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.btnOK.Location = new System.Drawing.Point(120, 176);
this.btnOK.Name = "btnOK";
this.btnOK.Size = new System.Drawing.Size(80, 32);
this.btnOK.TabIndex = 3;
this.btnOK.Text = "确定(&O)";
this.btnOK.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
//
// pictureBox2
//
this.pictureBox2.BackColor = System.Drawing.Color.Transparent;
this.pictureBox2.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("pictureBox2.BackgroundImage")));
this.pictureBox2.Location = new System.Drawing.Point(40, 128);
this.pictureBox2.Name = "pictureBox2";
this.pictureBox2.Size = new System.Drawing.Size(32, 32);
this.pictureBox2.TabIndex = 10;
this.pictureBox2.TabStop = false;
//
// txtPwd
//
this.txtPwd.Location = new System.Drawing.Point(88, 136);
this.txtPwd.Name = "txtPwd";
this.txtPwd.PasswordChar = '*';
this.txtPwd.Size = new System.Drawing.Size(208, 21);
this.txtPwd.TabIndex = 2;
this.txtPwd.Text = "";
//
// label2
//
this.label2.Location = new System.Drawing.Point(88, 112);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(80, 23);
this.label2.TabIndex = 8;
this.label2.Text = "操作员密码:";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// pictureBox1
//
this.pictureBox1.BackColor = System.Drawing.Color.Transparent;
this.pictureBox1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("pictureBox1.BackgroundImage")));
this.pictureBox1.Location = new System.Drawing.Point(40, 64);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(32, 32);
this.pictureBox1.TabIndex = 13;
this.pictureBox1.TabStop = false;
//
// label1
//
this.label1.Location = new System.Drawing.Point(88, 56);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(80, 23);
this.label1.TabIndex = 14;
this.label1.Text = "操作员姓名:";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// txtUser
//
this.txtUser.Location = new System.Drawing.Point(88, 80);
this.txtUser.Name = "txtUser";
this.txtUser.Size = new System.Drawing.Size(208, 21);
this.txtUser.TabIndex = 1;
this.txtUser.Text = "";
//
// pictureBox3
//
this.pictureBox3.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox3.Image")));
this.pictureBox3.Location = new System.Drawing.Point(0, 8);
this.pictureBox3.Name = "pictureBox3";
this.pictureBox3.Size = new System.Drawing.Size(352, 40);
this.pictureBox3.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox3.TabIndex = 15;
this.pictureBox3.TabStop = false;
//
// frmLogin
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(352, 221);
this.Controls.Add(this.pictureBox3);
this.Controls.Add(this.txtUser);
this.Controls.Add(this.txtPwd);
this.Controls.Add(this.label1);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnOK);
this.Controls.Add(this.pictureBox2);
this.Controls.Add(this.label2);
this.Name = "frmLogin";
this.Text = "用户登录";
this.Load += new System.EventHandler(this.frmLogin_Load);
this.ResumeLayout(false);
}
#endregion
private void frmLogin_Load(object sender, System.EventArgs e)
{
}
private void btnOK_Click(object sender, System.EventArgs e)
{
}
// public static string UserName()
// {
// UserName u= this.txtName.Text;
// }
public bool login()
{
if(this.txtUser.Text=="")
{
MessageBox.Show("请您输入用户名!","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return false;
}
else if(this.txtUser.Text==""&&this.txtPwd.Text=="")
{
MessageBox.Show("请您输入用户名!","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return false;
}
else if(this.txtPwd.Text=="")
{
MessageBox.Show("请您输入密码!","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
this.txtPwd.Focus();
return false;
}
else
{
string userPwd=this.txtPwd.Text;
SqlParameter para=new SqlParameter("@userName",SqlDbType.VarChar,50);
para.Value=this.txtUser.Text;
SqlConnection conn=new SqlConnection("server=.;database=DBApp;Trusted_Connection=true;");
conn.Open();
SqlCommand cmd = new SqlCommand("select count(*) from 用户清单 where 姓名=@userName", conn);
cmd.Parameters.Add(para);
int count = Convert.ToInt32(cmd.ExecuteScalar());
if(count==0)
{
MessageBox.Show("该用户不存在!","登录失败",MessageBoxButtons.OK,MessageBoxIcon.Warning);
this.txtPwd.Text="";
this.txtUser.Text="";
this.txtUser.Focus();
conn.Close();
return false;
}
else
{
cmd.CommandText="select 密码 from 用户清单 where 姓名=@userName";
string pwd=Convert.ToString(cmd.ExecuteScalar()).Trim();
if(pwd!=userPwd)
{
conn.Close();
MessageBox.Show("密码错误!","登录失败",MessageBoxButtons.OK,MessageBoxIcon.Warning);
this.txtPwd.Text="";
this.txtPwd.Focus();
return false;
}
else
{
conn.Close();
return true;
}
}
}
}
// public bool login()
// {
// SqlConnection conn=new SqlConnection("server=.;database=DBApp;Trusted_Connection=true;");
//
// SqlCommand cmd=new SqlCommand("select count(*) from 用户清单 where 姓名=@Name and 密码=@Password",conn);
// // try
// // {
// SqlParameter para;
//
// para = new SqlParameter("@Name",SqlDbType.VarChar,50);
// para.Value = this.txtName.Text;
// cmd.Parameters.Add(para);
//
// para = new SqlParameter("@Password",SqlDbType.VarChar,50);
// para.Value = this.txtPassword.Text;
// cmd.Parameters.Add(para);
//
// conn.Open();
// int input = Convert.ToInt32(cmd.ExecuteScalar());
//
// cmd.Parameters.Clear();
//
// if(input== 0)
// {
// MessageBox.Show("ssss");
// conn.Close();
// return false;
//
//
// }
// else
// {
// MessageBox.Show("服务器拒绝登录!!!","错误提示");
// conn.Close();
// return true;
//
// }
// // }
// // catch
// // {
// // MessageBox.Show("请检查你的输入是否正确!!!","错误提示");
// //
// // }
// }
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -