整体程序流程图
下面从登录窗口开始,逐个给出相应的窗体截图和对应代码
Login
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Login : System.Windows.Forms.Form
{
private string Scode;//存放验证码
private static char[] constant =
{
'0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
};//验证码单个字符的可能取值
public Login()
{
InitializeComponent();
this.Randomcode();
}
public static string EncryptWithMD5(string source)
{
byte[] sor = Encoding.UTF8.GetBytes(source);
MD5 md5 = MD5.Create();
byte[] result = md5.ComputeHash(sor);
StringBuilder strbul = new StringBuilder(40);
for (int i = 0; i < result.Length; i++)
{
strbul.Append(result[i].ToString("x2"));//加密结果"x2"结果为32位,"x3"结果为48位,"x4"结果为64位
}
return strbul.ToString();
}
public void Randomcode()
{
int i = 0;
Scode = "";
Random rand = new Random();
for (i = 0; i < 4; i++)
{
Scode += constant[rand.Next(0, 57)];
}
textBox1.BackColor = Color.FromArgb(rand.Next(200,225), rand.Next(200, 225), rand.Next(200, 225));
textBox1.ForeColor = Color.FromArgb(rand.Next(0,100), rand.Next(0, 100), rand.Next(0, 100));
textBox1.Text = Scode;
i = rand.Next(1, 3);
if(i==1)
{
textBox1.TextAlign = HorizontalAlignment.Center;
}
if (i == 2)
{
textBox1.TextAlign = HorizontalAlignment.Left;
}
if (i == 3)
{
textBox1.TextAlign = HorizontalAlignment.Right;
}
}
private void button1_Click(object sender, EventArgs e)
{
string code= textBox3.Text.Trim();
code=code.ToUpper();
Scode = Scode.ToUpper();
if (code!=Scode)
{
MessageBox.Show("验证码错误!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Randomcode();
}
else
{
string username = textBoxUserName.Text.Trim(); //取出账号
string password = EncryptWithMD5(textBoxPassWord.Text.Trim()); //取出密码
string myConnString = "Data Source=.;Initial Catalog=test;Persist Security Info=True;User ID=sa;Password=***********";
SqlConnection sqlConnection = new SqlConnection(myConnString); //实例化连接
sqlConnection.Open();
string sql = "select userid,password from usertable where userid = '" + username + "' and password = '" + password + "'"; //编写SQL命令
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
if (sqlDataReader.HasRows)
{
Program.user = username;
MessageBox.Show("欢迎!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); //登录成功
sqlConnection.Close();
sqlConnection = new SqlConnection(myConnString); //实例化连接
sqlConnection.Open();
sql = "select userid,password from usertable where userid = '" + username + "' and useridentity = '教师'"; //编写SQL命令
sqlCommand = new SqlCommand(sql, sqlConnection);
sqlDataReader = sqlCommand.ExecuteReader();
if (sqlDataReader.HasRows)
{
SQLForm sQLForm = new SQLForm();//新建一个主窗体
this.Hide();
DialogResult d = sQLForm.ShowDialog();//显示主窗体
if (d == DialogResult.OK)
{
this.Visible = true;
this.Randomcode();
}
else
this.Close();
}
else
{
LSQLForm sQLForm = new LSQLForm();//新建一个主窗体
this.Hide();
DialogResult d = sQLForm.ShowDialog();//显示主窗体
if (d == DialogResult.OK)
{
this.Visible = true;
this.Randomcode();
}
else
this.Close();
}
}
else
{
MessageBox.Show("账号或密码错误!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Randomcode();
}
sqlConnection.Close();//最后别忘了停止sql流程
}
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
this.Randomcode();
textBox1.Text = Scode;
}
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Register Form = new Register();//新建一个主窗体
Form.Show();
}
}
}
Register
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Register : Form
{
private string Scode;
private static char[] constant =
{
'0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
};
public Byte[] mybyte = new byte[0];
public Register()
{
InitializeComponent();
this.Randomcode();
}
public void Randomcode()
{
int i = 0;
Scode = "";
Random rand = new Random();
for (i = 0; i < 4; i++)
{
Scode += constant[rand.Next(0, 57)];
}
textBox6.BackColor = Color.FromArgb(rand.Next(200, 225), rand.Next(200, 225), rand.Next(200, 225));
textBox6.ForeColor = Color.FromArgb(rand.Next(0, 100), rand.Next(0, 100), rand.Next(0, 100));
textBox6.Text = Scode;
i = rand.Next(1, 3);
if