当前位置:网站首页>二、用户登录和注册
二、用户登录和注册
2022-06-11 07:03:00 【beyond谚语】
一、页面设计
一共四个页面
主页面Form1,登录页面login,注册页面resister,主菜单页面main_page
系统运行进入Form1,单击登录按钮跳转到login,数据库中得存在数据信息且输入正确才可登录成功,跳转到main_page
单击注册按钮跳转到register,用户需要注册账号、密码、性别、手机号信息
Form1
login
register
main_page
四个页面如下
二、数据库的创建
创建fiber_yy数据库,在其下创建yy_user表,其中id为主键自增
整体结构如下
三、相关代码
Form1代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
namespace fiber_yy
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label2_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
new login().Show();
}
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("see you again~");
Application.Exit(); //退出进程
}
private void button2_Click(object sender, EventArgs e)
{
this.Hide();
new register().Show();
}
}
}
login代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
namespace fiber_yy
{
public partial class login : Form
{
public static string str_conn = "server=CY-20190824RMES;Initial Catalog=fiber_yy;User ID=sa;pwd=beyond";
SqlConnection conn = new SqlConnection(str_conn);
public string identification = null;
public login()
{
InitializeComponent();
conn.Open();
}
private void button1_Click(object sender, EventArgs e)//登录
{
string username = textBox1.Text;
string password = textBox2.Text;
string identify = textBox3.Text;
if (username.Equals("") || password.Equals("") || identify.Equals(""))
{
MessageBox.Show("提示:请输入用户名、密码、验证码!", "警告");
}
else
{
string sqlSel = "select count(*) from yy_user where username = '" + username + "' and password = '" + password + "'";
SqlCommand cmd = new SqlCommand(sqlSel, conn);
if (Convert.ToInt32(cmd.ExecuteScalar()) > 0 )
{
if(identify==identification)
{
this.Close();
MessageBox.Show("用户登录成功");
new main_page().Show();
}
else
{
MessageBox.Show("验证码输入错误");
}
}
else
{
MessageBox.Show("账号或密码错误");
}
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
Random r = new Random();
string str = null;
for (int i = 0; i < 5; i++)
{
int n = r.Next(0, 10);
str += n;//包括字符串在内
}
identification = str;
Bitmap b = new Bitmap(100, 15);
Graphics g = Graphics.FromImage(b);
for (int i = 0; i < 5; i++)
{
String[] fonts = {
"宋体", "黑体", "隶书", "仿宋", "微软雅黑" };//字体数组
Color[] colors = {
Color.Red, Color.Black, Color.Blue,Color.YellowGreen ,Color.Green };//颜色数组
Font f = new Font(fonts[r.Next(0, 5)], 25, FontStyle.Bold);
SolidBrush s = new SolidBrush(colors[r.Next(0, 5)]);//定义一个单独的画笔,使每个字符的颜色随机
Point p = new Point(i * 20, 0);//每个字符间隔20
g.DrawString(str[i].ToString(), Font, s, p);
}
for (int a = 0; a < 5; a++)
{
Point p1 = new Point(r.Next(0, b.Width), r.Next(0, b.Height));
Point p2 = new Point(r.Next(0, b.Width), r.Next(0, b.Height));//线的两点不能超过图片的长和宽
Pen pen = new Pen(Brushes.Cyan);//青色线段
g.DrawLine(pen, p1, p2);
}
pictureBox1.Image = b;
}
}
}
register代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
namespace fiber_yy
{
public partial class register : Form
{
public static string str_conn = "server=CY-20190824RMES;Initial Catalog=fiber_yy;User ID=sa;pwd=beyond";
SqlConnection conn = new SqlConnection(str_conn);
public register()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string sex="man";
long phone_number;
int account_number, password, sum=0;
account_number = textBox1.Text.Length;
password = textBox2.Text.Length;
phone_number = long.Parse(textBox3.Text);
if (account_number <= 3 || account_number >= 16)
{
label6.Text = "账号长度应该在3~16字符之间";
sum++;
}
else
{
label6.Text = "校验成功";
}
if (password <= 3 || password >= 16)
{
label7.Text = "密码长度应该在3~16字符之间";
sum++;
}
else
{
label7.Text = "校验成功";
}
if (radioButton1.Checked)
{
sex = "man";
}
else if (radioButton2.Checked)
{
sex = "woman";
}
if (phone_number < 10000000000 || phone_number > 99999999999)
{
label9.Text = "请输入正确的手机号";
sum++;
}
else
{
label9.Text = "校验成功";
}
if (sum == 0)
{
label6.Text = "√";
label7.Text = "√";
label8.Text = "√";
label9.Text = "√";
try
{
string sql = string.Format("select count(*) from yy_user where username='{0}'", textBox1.Text);
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
int a = (int)cmd.ExecuteScalar();//返回一个值,看用户是否存在 1存在 0不存在
StringBuilder strsql = new StringBuilder();
//MessageBox.Show(a.ToString());
if(a==1)
{
MessageBox.Show("用户名已存在");
}
else
{
MessageBox.Show("okk");
//string INSERT_sql = string.Format("INSERT INTO user_yy VALUES ('{0}','{1}')", textBox1.Text,textBox2.Text);
string INSERT_sql = string.Format("INSERT INTO yy_user VALUES ('{0}','{1}','{2}','{3}')", textBox1.Text.Trim(), textBox2.Text.Trim(), sex, textBox3.Text.Trim());
SqlCommand INSERT_cmd = new SqlCommand(INSERT_sql, conn);
int count = INSERT_cmd.ExecuteNonQuery();
if (count > 0)
{
MessageBox.Show("注册成功!");
this.Close();
new Form1().Show();
}
else
{
MessageBox.Show("GG");
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
MessageBox.Show("用户登录成功");
new main_page().Show();
}
}
}
main_page代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace fiber_yy
{
public partial class main_page : Form
{
public main_page()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("退出成功");
this.Close();
new Form1().Show();
}
}
}
四、效果展示
原始数据集数据信息
运行程序
注册



右击 yy_user—> 选择前1000行
已经注册成功了,接下来开始登录
随便输入一个账号

输入正确的账号,但验证码输入错误

账号密码验证码输入正确

跳转到main_page
退出登录之后会返回到Form1

退出系统,提示see you again~
边栏推荐
- Detailed explanation of mutationobserver
- Quality-aware Feature Aggregation Networkfor Robust RGBT Tracking
- Analysis of key points and difficulties of ES6 promise source code
- [deploy private warehouse based on harbor] 4 push image to harbor
- 教育专家王中泽老师一招解决学生问题
- Saltstack deployment ZABBIX state file writing
- [deploy private warehouse based on harbor] 2 machine preparation
- 3.1 测试函数的命名规则
- 洛谷P1091合唱队形(最长上升子序列)
- About daily report plan
猜你喜欢

常用问题排查工具和分析神器,值得收藏

Senior openstacker - Bloomberg, vexxhost upgraded to the Gold member of openinfra Foundation
![[deploy private warehouse based on harbor] 4 push image to harbor](/img/af/8e28b229d94f3e6eab02308b69dc74.jpg)
[deploy private warehouse based on harbor] 4 push image to harbor

About the designer of qtcreator the solution to the problem that qtdesigner can't pull and hold controls normally

Saltstack deployment LNMP

WPF data binding (IV)

.NET C#基础(6):命名空间 - 有名字的作用域

洛谷P1091合唱队形(最长上升子序列)
![[matlab WSN communication] a_ Star improved leach multi hop transmission protocol [including source code phase 487]](/img/fd/53776b550390752282cd8797193436.png)
[matlab WSN communication] a_ Star improved leach multi hop transmission protocol [including source code phase 487]

Check whether the filing information of the medical representative is correct
随机推荐
A highly controversial issue
Matplotlib, set coordinate scale size, font / set legend size and font / set vertical and horizontal coordinate name, font and size
常用问题排查工具和分析神器,值得收藏
Senior openstacker - Bloomberg, vexxhost upgraded to gold member of openinfra Foundation
337. house raiding III
First day of database
Janus feature草稿
[MATLAB image encryption and decryption] chaotic sequence image encryption and decryption (including correlation test) [including GUI source code 1862]
【迅为干货】龙芯2k1000开发板opencv 测试
【LeetCode】-- 17. Letter combination of telephone number
The difference between arrow function and ordinary function
[advanced concurrency] - thread pool summary
Senior openstacker - Bloomberg, vexxhost upgraded to the Gold member of openinfra Foundation
Leetcode hot topic 100 topic 6-10 solution
Detailed explanation of mutationobserver
1266_FreeRTOS调度器启动代码实现分析
ESP32学习笔记(49)——ESP-WIFI-MESH接口使用
Xunwei dry goods | Ruixin micro rk3568 development board TFTP & NFS writing (Part 1)
资深OpenStacker - 彭博、Vexxhost升级为OpenInfra基金会黄金成员
VTK vtkplane and vtkcutter use