整体程序流程图
下面从登录窗口开始,逐个给出相应的窗体截图和对应代码
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 (i == 1)
{
textBox6.TextAlign = HorizontalAlignment.Center;
}
if (i == 2)
{
textBox6.TextAlign = HorizontalAlignment.Left;
}
if (i == 3)
{
textBox6.TextAlign = HorizontalAlignment.Right;
}
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
this.Randomcode();
}
private void button2_Click(object sender, EventArgs e)
{
//打开浏览图片对话框
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.ShowDialog();
string picturePath = openFileDialog.FileName;//获取图片路径
//文件的名称,每次必须更换图片的名称,这里很为不便
//创建FileStream对象
if(picturePath!="")
{
FileStream fs = new FileStream(picturePath, FileMode.Open, FileAccess.Read);
//声明Byte数组
mybyte = new byte[fs.Length];
//读取数据
fs.Read(mybyte, 0, mybyte.Length);
pictureBox1.Image = Image.FromStream(fs);
fs.Close();
}
}
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();
}
private void button1_Click(object sender, EventArgs e)
{
string code = textBox5.Text.Trim();
code = code.ToUpper();
Scode = Scode.ToUpper();
if (code != Scode)
{
MessageBox.Show("验证码错误!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Randomcode();
}
else
{
if(label9.Text!=""|| label8.Text != "")
{
MessageBox.Show("用户名或密码格式错误", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Randomcode();
}
else
{
if(comboBox1.Text=="")
{
MessageBox.Show("账户类型不能为空", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Randomcode();
}
else
{
if(pictureBox1.Image==null)
{
MessageBox.Show("头像不能为空", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Randomcode();
}
else
{
if (textBox1.Text == "")
{
MessageBox.Show("电话号码不能为空", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Randomcode();
}
else
{
if (textBox4.Text == "")
{
MessageBox.Show("学号或教职工号不能为空", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Randomcode();
}
else
{
try
{
string connString = "Data Source=.;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=***********";//数据库连接字符串
SqlConnection connection = new SqlConnection(connString);//创建connection对象
string sql = "insert into usertable (userid, password , number, phone, birthday , useridentity , userphoto ) " +
"values (@userid, @password,@number,@phone,@birthday,@useridentity,@userphoto)";
SqlCommand command = new SqlCommand(sql, connection);
SqlParameter sqlParameter = new SqlParameter("@userid", textBox3.Text.Trim());
command.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@password", EncryptWithMD5(textBox2.Text.Trim()));
command.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@number", textBox4.Text.Trim());
command.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@phone", textBox1.Text.Trim());
command.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@birthday", dateTimePicker1.Value);
command.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@useridentity", comboBox1.Text.Trim());
command.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@userphoto", SqlDbType.VarBinary, mybyte.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, mybyte);
command.Parameters.Add(sqlParameter);
//打开数据库连接
connection.Open();
command.ExecuteNonQuery();
connection.Close();
MessageBox.Show("注册成功", "notice", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
this.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
}
}
}
}
private void textBox3_Leave(object sender, EventArgs e)
{
string username = textBox3.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 from usertable where userid = '" + username + "'"; //编写SQL命令
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
if(sqlDataReader.HasRows)
{
label8.Text = "*该用户名已经存在";
}
else
{
if (textBox3.Text.Trim() != "")
{
label8.Text = "";
}
else
{
label8.Text = "*用户名不能为空";
}
//查询该用户名是否已经存在
}
sqlConnection.Close();
}
private void textBox2_Leave(object sender, EventArgs e)
{
if (textBox2.Text.Trim() != "")
{
//使用regex(正则表达式)进行格式设置 至少有数字、大写字母、小写字母各一个。最少3个字符、最长20个字符。
Regex regex = new Regex(@"(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{3,20}");
if (regex.IsMatch(textBox2.Text))//判断格式是否符合要求
{
label9.Text = "";
}
else
{
label9.Text = "*至少包含数字、大小写字母各一个。且不超过20个字符";
}
}
else
{
label9.Text = "*密码不能为空";
}
}
}
}
LSQLForm
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class LSQLForm : Form
{
public LSQLForm()
{
InitializeComponent();
}
private void LSQLForm_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“sinformationDataSet2.Student”中。您可以根据需要移动或删除它。
this.studentTableAdapter.Fill(this.sinformationDataSet2.Student);
}
private void 个人信息ToolStripMenuItem_Click(object sender, EventArgs e)
{
information Form = new information();//新建一个主窗体
Form.Show();//显示主窗体
}
private void 查询ToolStripMenuItem_Click(object sender, EventArgs e)
{
SelectForm Form = new SelectForm();//新建一个主窗体
Form.ShowDialog();//显示主窗体
if (Program.sql != "")
{
String conn = "Data Source =.; Initial Catalog = Sinformation; Persist Security Info = True; User ID = sa; Password = **********";
SqlConnection sqlConnection = new SqlConnection(conn); //实例化连接对象
try
{
sqlConnection.Open();
SqlCommand sqlCommand = new SqlCommand(Program.sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
}
finally
{
sqlConnection.Close();
}
}
this.studentTableAdapter.Fill(this.sinformationDataSet2.Student);
Program.sql = "";
}
private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
{
String conn = "Data Source =.; Initial Catalog = Sinformation; Persist Security Info = True; User ID = sa; Password = *********";
SqlConnection sqlConnection = new SqlConnection(conn);
string sql = "select * from Student";
sqlConnection.Open();
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
sqlConnection.Close();
}
private void 修改信息ToolStripMenuItem_Click(object sender, EventArgs e)
{
Updateinformation Form = new Updateinformation();//新建一个主窗体
Form.Show();//显示主窗体
}
private void 修改密码ToolStripMenuItem_Click(object sender, EventArgs e)
{
changepassword Form = new changepassword();
Form.Show();
}
private void 退出登录ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
}
}
information
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.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class information : Form
{
public information()
{
InitializeComponent();
string str = "Data Source =.; Initial Catalog = test; Persist Security Info = True; User ID = sa; Password = ************";
SqlConnection con = new SqlConnection(str);
con.Open();
string sql = "select * from usertable where userid='"+Program.user+"'";
SqlCommand com = new SqlCommand(sql, con);
SqlDataReader read = com.ExecuteReader(); //用com(变量名)点上ExecuteReader()方法,该方法的类型是SqlDataReader类型
read.Read();
textBox3.Text = read["userid"].ToString();//查询列名1的数据,方法为: read(变量名)["列名"]; 该方法返回的是object类型
textBox4.Text = read["number"].ToString(); //如上
textBox1.Text = read["phone"].ToString();
dateTimePicker1.Value = Convert.ToDateTime(read["birthday"]);
textBox2.Text = read["useridentity"].ToString();
con.Close();
con.Open();
com = new SqlCommand(sql, con);
SqlDataAdapter dataAdapter = new SqlDataAdapter(com);
//创建DataSet对象
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet, "SysUser");
int c = dataSet.Tables["SysUser"].Rows.Count;
if (c > 0)
{
Byte[] mybyte = new byte[0];
mybyte = (Byte[])(dataSet.Tables["SysUser"].Rows[c - 1]["UserPhoto"]);
MemoryStream ms = new MemoryStream(mybyte);
pictureBox1.Image = Image.FromStream(ms);
}
else
pictureBox1.Image = null;
con.Close();
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Updateinformation
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.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Updateinformation : 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 Updateinformation()
{
InitializeComponent();
string str = "Data Source =.; Initial Catalog = test; Persist Security Info = True; User ID = sa; Password = **********";
SqlConnection con = new SqlConnection(str);
con.Open();
string sql = "select * from usertable where userid='" + Program.user + "'";
SqlCommand com = new SqlCommand(sql, con);
SqlDataReader read = com.ExecuteReader(); //用com(变量名)点上ExecuteReader()方法,该方法的类型是SqlDataReader类型
read.Read();
textBox3.Text = read["userid"].ToString();//查询列名1的数据,方法为: read(变量名)["列名"]; 该方法返回的是object类型
textBox4.Text = read["number"].ToString(); //如上
textBox1.Text = read["phone"].ToString();
dateTimePicker1.Value = Convert.ToDateTime(read["birthday"]);
textBox2.Text = read["useridentity"].ToString();
con.Close();
con.Open();
com = new SqlCommand(sql, con);
SqlDataAdapter dataAdapter = new SqlDataAdapter(com);
//创建DataSet对象
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet, "SysUser");
int c = dataSet.Tables["SysUser"].Rows.Count;
if (c > 0)
{
mybyte = new byte[0];
mybyte = (Byte[])(dataSet.Tables["SysUser"].Rows[c - 1]["UserPhoto"]);
MemoryStream ms = new MemoryStream(mybyte);
pictureBox1.Image = Image.FromStream(ms);
}
else
pictureBox1.Image = null;
con.Close();
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 (i == 1)
{
textBox6.TextAlign = HorizontalAlignment.Center;
}
if (i == 2)
{
textBox6.TextAlign = HorizontalAlignment.Left;
}
if (i == 3)
{
textBox6.TextAlign = HorizontalAlignment.Right;
}
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
this.Randomcode();
}
private void button2_Click(object sender, EventArgs e)
{
//打开浏览图片对话框
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.ShowDialog();
string picturePath = openFileDialog.FileName;//获取图片路径
//文件的名称,每次必须更换图片的名称,这里很为不便
//创建FileStream对象
if (picturePath != "")
{
FileStream fs = new FileStream(picturePath, FileMode.Open, FileAccess.Read);
//声明Byte数组
mybyte = new byte[fs.Length];
//读取数据
fs.Read(mybyte, 0, mybyte.Length);
pictureBox1.Image = Image.FromStream(fs);
fs.Close();
}
}
private void button1_Click(object sender, EventArgs e)
{
string code = textBox5.Text.Trim();
code = code.ToUpper();
Scode = Scode.ToUpper();
if (code != Scode)
{
MessageBox.Show("验证码错误!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Randomcode();
}
else
{
if (textBox2.Text == "")
{
MessageBox.Show("账户类型不能为空", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Randomcode();
}
else
{
if (textBox1.Text == "")
{
MessageBox.Show("电话号码不能为空", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Randomcode();
}
else
{
if (textBox4.Text == "")
{
MessageBox.Show("学号或教职工号不能为空", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Randomcode();
}
else
{
try
{
string connString = "Data Source=.;Initial Catalog=test;Persist Security Info=True;User ID=sa;Password=*********";//数据库连接字符串
SqlConnection connection = new SqlConnection(connString);//创建connection对象
string sql = "UPDATe usertable SET number=@number,phone=@phone,birthday=@birthday,userphoto=@userphoto WHERe userid = '" + Program.user+"'";
SqlCommand command = new SqlCommand(sql, connection);
SqlParameter sqlParameter = new SqlParameter("@number", textBox4.Text.Trim());
command.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@phone", textBox1.Text.Trim());
command.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@birthday", dateTimePicker1.Value);
command.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@userphoto", SqlDbType.VarBinary, mybyte.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, mybyte);
command.Parameters.Add(sqlParameter);
//打开数据库连接
connection.Open();
command.ExecuteNonQuery();
connection.Close();
MessageBox.Show("修改成功");
this.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
}
}
private void button3_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
changepassword
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.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class changepassword : Form
{
public changepassword()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void textBox2_Leave(object sender, EventArgs e)
{
if (textBox2.Text.Trim() != "")
{
//使用regex(正则表达式)进行格式设置 至少有数字、大写字母、小写字母各一个。最少3个字符、最长20个字符。
Regex regex = new Regex(@"(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{3,20}");
if (regex.IsMatch(textBox2.Text))//判断格式是否符合要求
{
label4.Text = "";
}
else
{
label4.Text = "*至少包含数字、大小写字母各一个。且不超过20个字符";
}
}
else
{
label4.Text = "*密码不能为空";
}
}
private void textBox3_Leave(object sender, EventArgs e)
{
if (textBox2.Text.Trim() != "")
{
//使用regex(正则表达式)进行格式设置 至少有数字、大写字母、小写字母各一个。最少3个字符、最长20个字符。
Regex regex = new Regex(@"(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{3,20}");
if (regex.IsMatch(textBox2.Text))//判断格式是否符合要求
{
label5.Text = "";
}
else
{
label5.Text = "*至少包含数字、大小写字母各一个。且不超过20个字符";
}
}
else
{
label5.Text = "*密码不能为空";
}
}
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();
}
private void button1_Click(object sender, EventArgs e)
{
string password1, password2, password0;
password0 = textBox1.Text.Trim();
password1 = textBox2.Text.Trim();
password2 = textBox3.Text.Trim();
if(password0== password1)
{
MessageBox.Show("新密码与旧密码不能相同!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
if (label4.Text != "")
{
MessageBox.Show("新密码格式错误!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
if (password1 != password2)
{
MessageBox.Show("两次输入的新密码不同!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
string myConnString = "Data Source=.;Initial Catalog=test;Persist Security Info=True;User ID=sa;Password=**********";
SqlConnection sqlConnection = new SqlConnection(myConnString); //实例化连接
sqlConnection.Open();
password0 = EncryptWithMD5(password0);
string sql = "select userid,password from usertable where userid = '" + Program.user + "' and password = '" + password0 + "'"; //编写SQL命令
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
if (sqlDataReader.HasRows)
{
sqlConnection.Close();
password1 = EncryptWithMD5(password1);
sqlConnection = new SqlConnection(myConnString);
sqlConnection.Open();
sql = "UPDATe usertable SET password='" + password1 + "' WHERe userid = '" + Program.user + "'";
sqlCommand = new SqlCommand(sql, sqlConnection);
sqlCommand.ExecuteNonQuery();
MessageBox.Show("修改成功!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
this.Close();
}
else
{
MessageBox.Show("原密码错误!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
sqlConnection.Close();
}
}
}
}
}
}
SelectForm
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 WindowsFormsApp1
{
public partial class SelectForm : Form
{
public SelectForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string StuName = textBox2.Text.Trim();
string StuNum = textBox1.Text.Trim();
string StuSex = comboBox1.Text;
string StuAge = textBox3.Text.Trim();
string StuDept = comboBox2.Text;
Program.sql += "select * from Student where";
if(StuName!="")
{
if (Program.sql != "select * from Student where")
Program.sql += " and";
Program.sql += " Sname='"+ StuName+"'";
}
if (StuNum != "")
{
if (Program.sql != "select * from Student where")
Program.sql += " and";
Program.sql += " Sno='" + StuNum + "'";
}
if (StuSex != "")
{
if (Program.sql != "select * from Student where")
Program.sql += " and";
Program.sql += " Ssex='" + StuSex + "'";
}
if (StuAge != "")
{
if (Program.sql != "select * from Student where")
Program.sql += " and";
Program.sql += " Sage=" + StuAge;
}
if (StuDept != "")
{
if (Program.sql != "select * from Student where")
Program.sql += " and";
Program.sql += " Sdept='" + StuDept + "'";
}
if(Program.sql== "select * from Student where")
{
MessageBox.Show("查询不能为空!","notice", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
this.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
Program.sql = "";
this.Close();
}
}
}
SQLForm
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class SQLForm : System.Windows.Forms.Form
{
public SQLForm()
{
InitializeComponent();
}
private void SQLForm_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“sinformationDataSet.Student”中。您可以根据需要移动或删除它。
this.studentTableAdapter.Fill(this.sinformationDataSet.Student);
}
private void button1_Click(object sender, EventArgs e)
{
AddForm Form = new AddForm();//新建一个主窗体
Form.ShowDialog();//显示主窗体
this.studentTableAdapter.Fill(this.sinformationDataSet.Student);
}
private void button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source =.; Initial Catalog = Sinformation; Persist Security Info = True; User ID = sa; Password = ************");
try
{
con.Open();
string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//选择的当前行第一列的值,也就是ID
string delete_by_id = "delete from Student where Sno=" + select_id;//sql删除语句
SqlCommand cmd = new SqlCommand(delete_by_id, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("请选中要删除的行", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
con.Close();
}
this.studentTableAdapter.Fill(this.sinformationDataSet.Student);
}
private void button3_Click(object sender, EventArgs e)
{
try
{
string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//选择的当前行第一列的值,也就是ID
UpdateForm Form = new UpdateForm(select_id);//新建一个主窗体
Form.ShowDialog();//显示主窗体
this.studentTableAdapter.Fill(this.sinformationDataSet.Student);
}
catch
{
MessageBox.Show("请选中要修改的行", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
}
}
private void button5_Click(object sender, EventArgs e)
{
String conn = "Data Source =.; Initial Catalog = Sinformation; Persist Security Info = True; User ID = sa; Password = ***********";
SqlConnection sqlConnection = new SqlConnection(conn);
string sql = "select * from Student";
sqlConnection.Open();
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
sqlConnection.Close();
}
private void button4_Click(object sender, EventArgs e)
{
SelectForm Form = new SelectForm();//新建一个主窗体
Form.ShowDialog();//显示主窗体
if(Program.sql != "")
{
String conn = "Data Source =.; Initial Catalog = Sinformation; Persist Security Info = True; User ID = sa; Password = *********";
SqlConnection sqlConnection = new SqlConnection(conn); //实例化连接对象
try
{
sqlConnection.Open();
SqlCommand sqlCommand = new SqlCommand(Program.sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
}
finally
{
sqlConnection.Close();
}
}
this.studentTableAdapter.Fill(this.sinformationDataSet.Student);
Program.sql = "";
}
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
information Form = new information();//新建一个主窗体
Form.Show();//显示主窗体
}
private void 修改信息ToolStripMenuItem_Click(object sender, EventArgs e)
{
Updateinformation Form = new Updateinformation();//新建一个主窗体
Form.Show();//显示主窗体
}
private void 修改密码ToolStripMenuItem_Click(object sender, EventArgs e)
{
changepassword Form = new changepassword();
Form.Show();
}
private void 操作日志ToolStripMenuItem_Click(object sender, EventArgs e)
{
Log Form = new Log();
Form.Show();
}
private void 退出登录ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
}
}
AddForm
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class AddForm : System.Windows.Forms.Form
{
public AddForm()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
string StuName = textBox2.Text.Trim();
string StuNum = textBox1.Text.Trim();
string StuSex = comboBox1.Text;
string StuAge = textBox3.Text.Trim();
string StuDept = comboBox2.Text;
SqlConnection con = new SqlConnection("Data Source =.; Initial Catalog = Sinformation; Persist Security Info = True; User ID = sa; Password = ***********");
try
{
con.Open();
string ComStr = "INSERT INTO Student(Sno, Sname, Ssex, Sage, Sdept)" + "VALUES ('" + StuNum + "','" + StuName + "','" + StuSex + "'," + StuAge + ",'" + StuDept + "')";
SqlCommand cmd = new SqlCommand(ComStr, con);
cmd.ExecuteNonQuery();
MessageBox.Show("添加成功", "notice", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
this.Close();
}
catch
{
MessageBox.Show("请正确输入数据(各项不能为空,学号不能重复)", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
con.Close();
}
}
}
}
UpdateForm
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class UpdateForm : Form
{
private string select_id;
public UpdateForm()
{
InitializeComponent();
}
public UpdateForm(string select_id)
{
this.select_id = select_id;
InitializeComponent();
textBox1.Text = select_id;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string StuName = textBox2.Text.Trim();
string StuNum = textBox1.Text.Trim();
string StuSex = comboBox1.Text;
string StuAge = textBox3.Text.Trim();
string StuDept = comboBox2.Text;
SqlConnection con = new SqlConnection("Data Source =.; Initial Catalog = Sinformation; Persist Security Info = True; User ID = sa; Password = *********");
try
{
con.Open();
string ComStr = "UPDATE Student SET Sname='" + StuName + "',Ssex='" + StuSex + "',Sage=" + StuAge + ",Sdept='" + StuDept + "'"+" WHERe Sno='"+StuNum+"'";
SqlCommand cmd = new SqlCommand(ComStr, con);
cmd.ExecuteNonQuery();
MessageBox.Show("修改成功", "notice", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
this.Close();
}
catch
{
MessageBox.Show("请正确输入数据", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
con.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
实际上删除并非是SQLForm的一个子窗体,但是由于其功能与其他几项权重相等,所以才单独标识在流程图中。删除的代码已经放入了SQLForm的窗体代码中。
下面是数据库中连接的几张表的截图
Student(学生信息表)
SysLog(操作日志)
usertable(账户信息)
最后给出Student与SysLog触发器的代码
ALTER TRIGGER [dbo].[regist_recorder]
ON [dbo].[Student]
AFTER
INSERT
AS
declare @UserName nchar(20)
declare @DateTime datetime
declare @UserOperation nchar(200)
select @UserName = system_user
select @DateTime = CONVERT(datetime,GETDATE(),120)
declare @op varchar(10)
select @op=case when exists(select 1 from inserted) and exists(select 1 from deleted)
then 'Update'
when exists(select 1 from inserted) and not exists(select 1 from deleted)
then 'Insert'
when not exists(select 1 from inserted) and exists(select 1 from deleted)
then 'Delete' end
select @UserOperation = @op
INSERT INTO SysLog(UserID,DateAndTime,UserOperation)
VALUES (@UserName,@DateTime,@UserOperation)



