一、页面设计
一共四个页面
主页面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);//每个字符间隔20g.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~