登录窗口
主要代码
生成验证码
public string yanzhengma = "";
private void Login_yanzheng(object sender, EventArgs e)
{
Random ran = new Random();
int number;
char code1;
for (int i = 0; i < 5; i++)//取五个数
{
number = ran.Next();
if (number % 2 == 0)
code1 = (char)('0' + (char)(number % 10));
else
code1 = (char)('A' + (char)(number % 26)); //转化为字符
yanzhengma += code1.ToString();
}
label5.Text = yanzhengma;//在窗体显示
}
登录界面的静态变量 zh, 用来记录账号。
public static string zh="";
登录按钮触发事件
(根据单选框的选择决定身份,默认学生,登录成功后打开新窗体)
private void button1_Click(object sender, EventArgs e)
{
string username = textBox1.Text.Trim(); //取出账号
string password = EncryptWithMD5(textBox2.Text.Trim()); //取出密码并加密
string myConnString = "Data Source=.;Initial Catalog=Text;User ID=sa;Password=155872";
SqlConnection sqlConnection = new SqlConnection(myConnString);
sqlConnection.Open();
if(radioButton1.Checked==true)
{
string sql = "select Stid,userPassword from Stuid where Stid = '" + username + "' and userPassword = '" + password + "'"; //编写SQL命令
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
if (sqlDataReader.HasRows && textBox3.Text == yanzhengma)
{
MessageBox.Show("登陆成功");
zh = textBox1.Text;
this.Hide();
Form3 form3 = new Form3();
form3.Show();
}
else
{
MessageBox.Show("登陆失败");
}
sqlDataReader.Close();
sql = "insert into SysLog values ( '" + username + "' , '" + DateTime.Now + "' , '" + "登录" + "','学生" + "')"; //编写SQL命令,添加登录记录
sqlCommand = new SqlCommand(sql, sqlConnection);
sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
}
else if(radioButton2.Checked==true)
{
string sql = "select Tid,userPassword from Tid where Tid = '" + username + "' and userPassword = '" + password + "'"; //编写SQL命令
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
if (sqlDataReader.HasRows && textBox3.Text == yanzhengma)
{
MessageBox.Show("登陆成功");
zh = textBox1.Text;
this.Hide();
Form4 form4 = new Form4();
form4.Show();
}
else
{
MessageBox.Show("登陆失败");
}
sqlDataReader.Close();
sql = "insert into SysLog values ( '" + username + "' , '" + DateTime.Now + "' , '" + "登录" + "','教师" + "')"; //编写SQL命令,添加登录记录
sqlCommand = new SqlCommand(sql, sqlConnection);
sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
}
else
{
string sql = "select Mid,userPassword from Masterid where Mid = '" + username + "' and userPassword = '" + password + "'"; //编写SQL命令
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
if (sqlDataReader.HasRows && textBox3.Text == yanzhengma)//验证码加注释
{
MessageBox.Show("登陆成功");
zh = textBox1.Text;
this.Hide();
Form5 form5 = new Form5();
form5.Show();
}
else
{
MessageBox.Show("登陆失败");
}
sqlDataReader.Close();
sql = "insert into SysLog values ( '" + username + "' , '" + DateTime.Now + "' , '" + "登录" +"','管理员"+ "')"; //编写SQL命令,添加登录记录
sqlCommand = new SqlCommand(sql, sqlConnection);
sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
}
}
激活账号界面
验证码同上
加密函数
private 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 Byte[] mybyte = new byte[0];
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.ShowDialog();
string picturePath = openFileDialog.FileName;//获取图片路径
//创建FileStream对象
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 button2_Click(object sender, EventArgs e)
{
if(radioButton1.Checked==true)
{
try
{
string connString = "Data Source=.;Initial Catalog= Text;Persist Security Info=True;User ID=sa;Password=155872";//数据库连接字符串
SqlConnection connection = new SqlConnection(connString);//创建connection对象
string sql = "insert into Stuid (Stid,userPassword,identify,Photo) " +
"values (@userid, @userpassword,@useridentity,@userphoto)";
SqlCommand command = new SqlCommand(sql, connection);
SqlParameter sqlParameter = new SqlParameter("@userid", textBox1.Text);
command.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@userpassword", EncryptWithMD5(textBox2.Text));
command.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@useridentity", "学生");
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);
//打开数据库连接
if (mybyte.Length == 0)
{
MessageBox.Show("请上传你的照片!");
}
else
{
if (textBox2.Text != "" && textBox3.Text == yanzhengma)
{
connection.Open();
command.ExecuteNonQuery();
connection.Close();
MessageBox.Show("激活成功");
this.Close();
}
else
{
if (textBox3.Text != yanzhengma)
{
MessageBox.Show("验证码错误!");
}
else
{
MessageBox.Show("密码不能为空");
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
try
{
string connString = "Data Source=.;Initial Catalog= Text;Persist Security Info=True;User ID=sa;Password=155872";//数据库连接字符串
SqlConnection connection = new SqlConnection(connString);//创建connection对象
string sql = "insert into Tid (Tid,userPassword,identify,Photo) " +
"values (@userid, @userpassword,@useridentity,@userphoto)";
SqlCommand command = new SqlCommand(sql, connection);
SqlParameter sqlParameter = new SqlParameter("@userid", textBox1.Text);
command.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@userpassword", EncryptWithMD5(textBox2.Text));
command.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@useridentity", "教师");
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);
//打开数据库连接
if (mybyte.Length == 0)
{
MessageBox.Show("请上传你的照片!");
}
else
{
if (textBox2.Text != "" && textBox3.Text == yanzhengma)
{
connection.Open();
command.ExecuteNonQuery();
connection.Close();
MessageBox.Show("激活成功");
this.Close();
}
else
{
if (textBox3.Text != yanzhengma)
{
MessageBox.Show("验证码错误!");
}
else
{
MessageBox.Show("密码不能为空");
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
学生登录界面
加载时展示新窗台(显示信息),将学生界面设为父窗体,子窗体外框去掉。
Form3()
{
InitializeComponent();
Schildform1 schildform1 = new Schildform1();
schildform1.MdiParent = this;
schildform1.FormBorderStyle = FormBorderStyle.None;
this.panel1.Controls.Clear();
this.panel1.Controls.Add(schildform1);
schildform1.Show();
}
显示信息窗体的代码:
private void Schildform1_Load(object sender, EventArgs e)
{
label8.Text = Form1.zh;
string connString1 = "Data Source=.;Initial Catalog=Text;Persist Security Info=True;User ID=sa;Password=155872";//数据库连接字符串
SqlConnection connection1 = new SqlConnection(connString1);//创建connection对象 //打开数据库连接
connection1.Open();
//显示信息
try
{
string sql= "select * from Student where Sno = '" + label8.Text + "'";
SqlCommand con = new SqlCommand(sql, connection1);
SqlDataAdapter dataAdapter = new SqlDataAdapter(con);
//创建DataSet对象
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet, "Sno");
int c1 = dataSet.Tables["Sno"].Rows.Count;
if(c1==1)
{
label9.Text = dataSet.Tables["Sno"].Rows[0].ItemArray[1].ToString();
label10.Text = dataSet.Tables["Sno"].Rows[0].ItemArray[2].ToString();
label11.Text = dataSet.Tables["Sno"].Rows[0].ItemArray[3].ToString();
label12.Text = dataSet.Tables["Sno"].Rows[0].ItemArray[4].ToString();
label13.Text = dataSet.Tables["Sno"].Rows[0].ItemArray[5].ToString();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
//显示照片
try
{
//创建SQL语句
String sql1 = "select Photo from Stuid where Stid = '" + label8.Text + "'";
//创建SqlCommand对象
SqlCommand command1 = new SqlCommand(sql1, connection1);
//创建DataAdapter对象
SqlDataAdapter dataAdapter1 = new SqlDataAdapter(command1);
//创建DataSet对象
DataSet dataSet1 = new DataSet();
dataAdapter1.Fill(dataSet1, "Stid");
int c1 = dataSet1.Tables["Stid"].Rows.Count;
if (c1 > 0)
{
Byte[] mybyte = new byte[0];
mybyte = (Byte[])(dataSet1.Tables["Stid"].Rows[c1 - 1]["Photo"])