using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace MyVideo
{
public partial class frmLogin : Form
{
public frmLogin()
{
InitializeComponent();
}
private void frmLogin_Load(object sender, EventArgs e)
{
this.timer1.Start(); //启动时间
}
//确定
private void btQueding_Click(object sender, EventArgs e)
{
if (tbName.Text.Trim() != "" && tbPas.Text.Trim() != "")//信息输入框都不为空
{
string LJ= @"Data Source=PC-20150826CYWO;Initial Catalog=MyData;Integrated Security=True";//建立连接
SqlConnection conn = new SqlConnection(LJ);
SqlCommand comm = conn.CreateCommand();//实例化数据库的操作命令
conn.Open();
if (rbStudent.Checked)//学生
{
string sql = "select sno,spass,sname from student where(sno='" + tbName.Text + "')";
comm.CommandText = sql;
SqlDataReader datar = comm.ExecuteReader();
//使 SqlDataReader 前进到下一条记录。返回一个bool值,如果为真跳出循环
while (datar.Read())
{
//读取数据库的内容,并与输入的进行比较;判断用户输入是否正确
if (datar["spass"].ToString().Trim() != tbPas.Text.Trim())
{
MessageBox.Show("密码不正确,请重新输入!");
tbPas.Focus();//焦点:使光标聚焦在文本输入框
return;
}
else
{
Form1 f1 = new Form1();
this.Hide();
//this.Close();
f1.ShowDialog();//信息正确,界面跳转
}
}
datar.Close();
}
else if (rbTeacher.Checked)//老师
{
string sql = "select tno,tpass,tname from teacher where(tno='" + tbName.Text + "')";
comm.CommandText = sql;
SqlDataReader datar = comm.ExecuteReader();
while (datar.Read())
{
//判断用户输入是否正确
if (datar["tpass"].ToString().Trim() != tbPas.Text.Trim())
{
MessageBox.Show("用户名或密码不正确,请重新输入!");
tbPas.Focus();
return;
}
else
{
Form1 f1 = new Form1();
this.Hide();
//this.Close();
f1.ShowDialog();
}
}
datar.Close();
}
else if (rbAdmin.Checked)//管理员
{
string sql = "SELECT adminname,adminpass,adminID FROM admin WHERE(adminID='" + tbName.Text + "')";
comm.CommandText = sql;
SqlDataReader datar = comm.ExecuteReader();
while (datar.Read())
{
if (datar["adminpass"].ToString().Trim() != tbPas.Text.Trim())
{
MessageBox.Show("密码不正确,请重新输入!");
tbPas.Focus();
return;
}
else
{
AdminLogin f2 = new AdminLogin();
this.Hide();
f2.ShowDialog();
}
}
datar.Close();
}
tbName.Text = "";//用户信息错误,信息清除
tbPas.Text = "";
}
else
{
MessageBox.Show("用户或密码不能为空!");
tbName.Focus();
return;
}
}
//取消
private void btQuxiao_Click(object sender, EventArgs e)
{
if (MessageBox.Show("你确定退出系统吗?", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK)
{
Application.Exit();
}
}
//时间显示
private void timer1_Tick(object sender, EventArgs e)
{
time.Text = "当前时间为:" + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString();
//字幕滚动
#region
this.time.Left -= 3;
if (this.time.Left < 0)
{
this.time.Left = this.Width-200;
}
#endregion
}
}
}