一、頁面設計
一共四個頁面
主頁面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~