📄 login.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace StudentInfoMS
{
public partial class login : Form
{
public login()
{
InitializeComponent();
}
private void btn_ok_Click(object sender, EventArgs e)
{
string connstr = "Data Source=localhost;Initial Catalog=mystuInfoDB;Integrated Security= true;";
//返回用户数量,如果为0,则用户id不存在
string cmduserid = "select count(userid) from login where userid='" + textBox1.Text.ToString() + "'";
//返回正确用户密码
string cmdstr = "select password from login where userid='" + textBox1.Text.ToString() + "'";
SqlCommand cmd=new SqlCommand();
SqlConnection mysqlconnection = new SqlConnection(connstr);
if (textBox1.Text.Length==0)
{
MessageBox.Show("请输入您的用户名!", "注意", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
textBox1.Focus();
}
else if (textBox2.Text.Length == 0)
{
MessageBox.Show("请输入您的密码!", "注意", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
textBox2.Focus();
}
else
{
try
{
cmd = mysqlconnection.CreateCommand();
cmd.CommandText=cmduserid;
mysqlconnection.Open();
int length=int.Parse(cmd.ExecuteScalar().ToString());//返回userid的长度
mysqlconnection.Close();
if (length == 0)//如果userid为0 则用户名不存在
{
MessageBox.Show("用户名不存在,请重新输入!", "注意", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
textBox1.Focus();
textBox2.Text = null;
}
else{
cmd.CommandText = cmdstr;
mysqlconnection.Open();
string pswd=cmd.ExecuteScalar().ToString().Trim();
mysqlconnection.Close();
if (textBox2.Text.ToString().Trim().Equals(pswd))
{
MessageBox.Show("Welcome "+textBox1.Text.ToString().Trim()+"!");
Form1 stuinfoms=new Form1();
stuinfoms.Show();
this.Hide();
}
else
{
MessageBox.Show("密码不正确,请重新输入","",MessageBoxButtons.OK,MessageBoxIcon.Stop);
textBox2.Text=null;
textBox2.Focus();
}
}
}
catch (Exception ex)
{
MessageBox.Show("连接数据库出错", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
mysqlconnection.Close();
}
}
}
private void btn_cancel_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void btn_reset_Click(object sender, EventArgs e)
{
textBox1.Text = null;
textBox2.Text = null;
textBox1.Focus();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -