1.窗体设计:
点击“登录”按钮以后连接数据库,验证用户输入信息是否正确,如正确,打开主窗体,隐藏登录窗体;如不正确,给出提示信息。点击“退出”按钮,关闭程序。
2.创建公共类DataAccess:
[Visual Basic .NET] 纯文本查看 复制代码 class DataAccess
{
string conStr;
OleDbConnection con;
//连接数据库
public void dataCon()
{
conStr = "Provider=Microsoft.Jet.OleDB.4.0;Data Source=" + Application.StartupPath + "\\student.mdb";
con = new OleDbConnection(conStr);
}
//查询数据库
public DataSet getDataSet(string sql)
{
DataSet ds = new DataSet();
try
{
con.Open();
OleDbCommand cmd = new OleDbCommand(sql, con);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(ds);
return ds;
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
finally
{
con.Close();
}
}
public bool sqlExec(string sql)
{
try
{
con.Open();
}
catch
{
MessageBox.Show("数据库未连接!");
}
try
{
OleDbCommand oledbCom = new OleDbCommand(sql, con);
oledbCom.ExecuteNonQuery();
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "提示");
return false;
}
finally
{
con.Close();
}
}
}
3.“登录”按钮及“退出”按钮点击事件:
[Visual Basic .NET] 纯文本查看 复制代码 public static string Uname;
public static string Upsw;
private void BtLogin_Click(object sender, EventArgs e)
{
string username = txtUsername.Text.Trim();
string password = txtPassword.Text.Trim();
if (txtUsername.Text.Trim() != "" && txtPassword.Text.Trim()!=" ")
{
//连接数据库
DataAccess data = new DataAccess();
data.dataCon();
//查询数据库
string cmdStr = "Select * from Userinfo where Userid='"+username+"' and Userpwd='"+password+"'";
DataSet ds;
ds = data.getDataSet(cmdStr);
if (ds.Tables[0].Rows.Count == 1)
{
Uname = txtUsername.Text.Trim();
Upsw = txtPassword.Text.Trim();
FormMain fmain = new FormMain();
if (ds.Tables[0].Rows[0]["Userlevel"].ToString() == "学生" && level.SelectedIndex == 0)
{
fmain.学生信息添加ToolStripMenuItem.Enabled = false;
fmain.学生信息修改ToolStripMenuItem.Enabled = false;
fmain.学生信息删除ToolStripMenuItem.Enabled = false;
fmain.课程信息ToolStripMenuItem.Enabled = false;
fmain.课程信息修改ToolStripMenuItem.Enabled = false;
fmain.课程信息删除ToolStripMenuItem.Enabled = false;
fmain.成绩添加ToolStripMenuItem.Enabled = false;
fmain.成绩删除ToolStripMenuItem.Enabled = false;
fmain.成绩修改ToolStripMenuItem.Enabled = false;
fmain.Show();
this.Hide();
}
else if (ds.Tables[0].Rows[0]["Userlevel"].ToString() == "教师" && level.SelectedIndex == 1)
{
fmain.Show();
this.Hide();
}
else
{
MessageBox.Show("权限选择错误!!");
}
}else
{
MessageBox.Show("账号或密码错误!");
}
}
else
{
MessageBox.Show("用户名和密码不能为空!");
}
}
private void BtCanel_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void FormLogin_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
private void FormLogin_Load(object sender, EventArgs e)
{
level.SelectedIndex = 0;
}
private void linkLabel_Regist_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
FormRegist Regist = new FormRegist();
Regist.Show();
}
4.主窗体:
通过点击各个子菜单项及工具栏按钮打开相应子窗体。
5.主窗体菜单、工具按钮点击事件:
[Visual Basic .NET] 纯文本查看 复制代码 private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
private void FormMain_Load(object sender, EventArgs e)
{
timer1.Interval = 1000;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
this.toolStripStatusLabel3.Text = "登录时间:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
}
private void 学生信息查询ToolStripMenuItem_Click(object sender, EventArgs e)
{
frmManageStu ManageStu = new frmManageStu();
string username = FormLogin.Uname;
string password = FormLogin.Upsw;
DataAccess data = new DataAccess();
data.dataCon();
string cmdStr = "Select * from Userinfo where Userid='" + username + "' and Userpwd='" + password + "'";
DataSet ds;
ds = data.getDataSet(cmdStr);
if (ds.Tables[0].Rows[0]["Userlevel"].ToString() == "学生" )
{
ManageStu.btnDel.Enabled = false;
ManageStu.btnEdit.Enabled = false;
}
ManageStu.MdiParent = this;
ManageStu.Show();
}
private void 学生信息添加ToolStripMenuItem_Click(object sender, EventArgs e)
{
frmAddStu Addstu = new frmAddStu();
Addstu.MdiParent = this;
Addstu.Show();
}
private void 学生信息修改ToolStripMenuItem_Click(object sender, EventArgs e)
{
frmManageStu ManageStu = new frmManageStu();
ManageStu.MdiParent = this;
ManageStu.Show();
}
private void 学生信息删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
frmManageStu ManageStu = new frmManageStu();
ManageStu.MdiParent = this;
ManageStu.Show();
}
private void 课程信息查询ToolStripMenuItem_Click(object sender, EventArgs e)
{
frmManageCourse ManageCourse = new frmManageCourse();
string username = FormLogin.Uname;
string password = FormLogin.Upsw;
DataAccess data = new DataAccess();
data.dataCon();
string cmdStr = "Select * from Userinfo where Userid='" + username + "' and Userpwd='" + password + "'";
DataSet ds;
ds = data.getDataSet(cmdStr);
if (ds.Tables[0].Rows[0]["Userlevel"].ToString() == "学生")
{
ManageCourse.btnDel.Enabled = false;
ManageCourse.btnEdit.Enabled = false;
}
ManageCourse.MdiParent = this;
ManageCourse.Show();
}
private void 课程信息修改ToolStripMenuItem_Click(object sender, EventArgs e)
{
frmAddCourse AddCourse = new frmAddCourse();
AddCourse.MdiParent = this;
AddCourse.Show();
}
private void 课程信息ToolStripMenuItem_Click(object sender, EventArgs e)
{
frmManageCourse ManageCourse = new frmManageCourse();
ManageCourse.MdiParent = this;
ManageCourse.Show();
}
private void 课程信息删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
frmManageCourse ManageCourse = new frmManageCourse();
ManageCourse.MdiParent = this;
ManageCourse.Show();
}
private void 成绩查询ToolStripMenuItem_Click(object sender, EventArgs e)
{
frmManageScore ManageScore = new frmManageScore();
string username = FormLogin.Uname;
string password = FormLogin.Upsw;
DataAccess data = new DataAccess();
data.dataCon();
string cmdStr = "Select * from Userinfo where Userid='" + username + "' and Userpwd='" + password + "'";
DataSet ds;
ds = data.getDataSet(cmdStr);
if (ds.Tables[0].Rows[0]["Userlevel"].ToString() == "学生")
{
ManageScore.btnDel.Enabled = false;
ManageScore.btnEdit.Enabled = false;
}
ManageScore.MdiParent = this;
ManageScore.Show();
}
private void 成绩修改ToolStripMenuItem_Click(object sender, EventArgs e)
{
frmManageScore ManageScore = new frmManageScore();
ManageScore.MdiParent = this;
ManageScore.Show();
}
private void 成绩删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
frmManageScore ManageScore = new frmManageScore();
ManageScore.MdiParent = this;
ManageScore.Show();
}
private void 成绩添加ToolStripMenuItem_Click(object sender, EventArgs e)
{
frmAddCredit AddCredit = new frmAddCredit();
AddCredit.MdiParent = this;
AddCredit.Show();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
frmManageStu ManageStu = new frmManageStu();
string username = FormLogin.Uname;
string password = FormLogin.Upsw;
DataAccess data = new DataAccess();
data.dataCon();
string cmdStr = "Select * from Userinfo where Userid='" + username + "' and Userpwd='" + password + "'";
DataSet ds;
ds = data.getDataSet(cmdStr);
if (ds.Tables[0].Rows[0]["Userlevel"].ToString() == "学生")
{
ManageStu.btnDel.Enabled = false;
ManageStu.btnEdit.Enabled = false;
}
ManageStu.MdiParent = this;
ManageStu.Show();
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
frmManageCourse ManageCourse = new frmManageCourse();
string username = FormLogin.Uname;
string password = FormLogin.Upsw;
DataAccess data = new DataAccess();
data.dataCon();
string cmdStr = "Select * from Userinfo where Userid='" + username + "' and Userpwd='" + password + "'";
DataSet ds;
ds = data.getDataSet(cmdStr);
if (ds.Tables[0].Rows[0]["Userlevel"].ToString() == "学生")
{
ManageCourse.btnDel.Enabled = false;
ManageCourse.btnEdit.Enabled = false;
}
ManageCourse.MdiParent = this;
ManageCourse.Show();
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
frmManageScore ManageScore = new frmManageScore();
string username = FormLogin.Uname;
string password = FormLogin.Upsw;
DataAccess data = new DataAccess();
data.dataCon();
string cmdStr = "Select * from Userinfo where Userid='" + username + "' and Userpwd='" + password + "'";
DataSet ds;
ds = data.getDataSet(cmdStr);
if (ds.Tables[0].Rows[0]["Userlevel"].ToString() == "学生")
{
ManageScore.btnDel.Enabled = false;
ManageScore.btnEdit.Enabled = false;
}
ManageScore.MdiParent = this;
ManageScore.Show();
}
private void 关于AToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("终于完工了。。。开森 ^ _ ^");
}
private void 修改密码PToolStripMenuItem_Click(object sender, EventArgs e)
{
frmPswChange PC = new frmPswChange();
PC.MdiParent = this;
PC.Show();
}
6.学生信息添加窗体。能够将学生信息添加到数据库中。
按钮点击事件:
[Visual Basic .NET] 纯文本查看 复制代码 private void btnAdd_Click(object sender, EventArgs e)
{
string strSql;
DataAccess data = new DataAccess();
strSql = "insert into Studentinfo (Sid,Sname,Sex,Birthday,Class,Tel,Address) values ('" + txtSid.Text + "','" + txtSname.Text + "','" + cboSex.Text + "','" + txtBirthday.Text + "','" + txtClass.Text + "','" + txtTel.Text + "','" + txtAddress.Text + "')";
data.dataCon();
if (data.sqlExec(strSql))
{
MessageBox.Show("添加成功!!");
}
else
{
MessageBox.Show("添加失败!!");
}
}
private void btnCancle_Click(object sender, EventArgs e)
{
this.Close();
}
7.学生信息管理窗体。能够“查询”、“删除”及“修改”学生信息。
按钮点击事件:
[Visual Basic .NET] 纯文本查看 复制代码 public frmManageStu()
{
InitializeComponent();
check();
}
public static string sid;
public static string Sid
{
get { return sid; }
set { sid = value; }
}
public void check()
{
string username = FormLogin.Uname;
string password = FormLogin.Upsw;
DataAccess data1 = new DataAccess();
data1.dataCon();
string cmdStr = "Select * from Userinfo where Userid='" + username + "' and Userpwd='" + password + "'";
DataSet ds1;
ds1 = data1.getDataSet(cmdStr);
if (ds1.Tables[0].Rows[0]["Userlevel"].ToString() == "学生")
{
string strSql;
DataAccess data = new DataAccess();
DataSet ds;
strSql = "select Sid as 学号,Sname as 姓名,Sex as 性别,Birthday as 出生日期,Class as 班级,Tel as 电话,Address as 家庭地址 from Studentinfo where Sid='" + username + "'";
data.dataCon();
ds = data.getDataSet(strSql);
txtSid.Text = ds.Tables[0].Rows[0][0].ToString();
txtSid.Enabled = false;
txtSname.Text = ds.Tables[0].Rows[0][1].ToString();
txtSname.Enabled = false;
cboSex.Text = ds.Tables[0].Rows[0][2].ToString();
cboSex.Enabled = false;
txtClass.Text = ds.Tables[0].Rows[0][4].ToString();
txtClass.Enabled = false;
}
}
private void btnSeach_Click(object sender, EventArgs e)
{
string username = FormLogin.Uname;
string password = FormLogin.Upsw;
DataAccess data1 = new DataAccess();
data1.dataCon();
string cmdStr = "Select * from Userinfo where Userid='" + username + "' and Userpwd='" + password + "'";
DataSet ds1;
ds1 = data1.getDataSet(cmdStr);
if (ds1.Tables[0].Rows[0]["Userlevel"].ToString() == "学生")
{
string strSql;
DataAccess data = new DataAccess();
DataSet ds;
strSql = "select Sid as 学号,Sname as 姓名,Sex as 性别,Birthday as 出生日期,Class as 班级,Tel as 电话,Address as 家庭地址 from Studentinfo where Sid='"+username+"'";
data.dataCon();
ds = data.getDataSet(strSql);
dgvInfo.DataSource = ds.Tables[0];
}
else
{
string strSql;
string condition = "";
DataAccess data = new DataAccess();
DataSet ds;
if (txtSid.Text != "")
{
condition += "and Sid='" + txtSid.Text + "'";
}
if (txtSname.Text != "")
{
condition += "and Sname='" + txtSname.Text + "'";
}
if (txtClass.Text != "")
{
condition += "and Class='" + txtClass.Text + "'";
}
if (cboSex.Text != "")
{
condition += "and Sex='" + cboSex.Text + "'";
}
strSql = "select Sid as 学号,Sname as 姓名,Sex as 性别,Birthday as 出生日期,Class as 班级,Tel as 电话,Address as 家庭地址 from Studentinfo where 1=1" + condition;
data.dataCon();
ds = data.getDataSet(strSql);
dgvInfo.DataSource = ds.Tables[0];
}
}
private void btnDel_Click(object sender, EventArgs e)
{
if (dgvInfo.CurrentRow == null)
{
MessageBox.Show("请先查询!!");
}
else
{
string strSql;
DataAccess data = new DataAccess();
strSql = "delete from Studentinfo where Sid='" + dgvInfo.CurrentRow.Cells[0].Value.ToString() + "'";
data.dataCon();
if (data.sqlExec(strSql))
{
string Strsql;
DataAccess data1 = new DataAccess();
DataSet ds;
Strsql = "select Sid as 学号,Sname as 姓名,Sex as 性别,Birthday as 出生日期,Class as 班级,Tel as 电话,Address as 家庭地址 from Studentinfo where 1=1";
data1.dataCon();
ds = data1.getDataSet(Strsql);
dgvInfo.DataSource = ds.Tables[0];
MessageBox.Show("删除成功!!");
}
else
{
MessageBox.Show("删除失败!!");
}
}
}
private void btnEdit_Click(object sender, EventArgs e)
{
if (dgvInfo.CurrentRow == null)
{
MessageBox.Show("请先查询!!");
}
else
{
frmManageStu.sid = dgvInfo.CurrentRow.Cells[0].Value.ToString();
frmEditStu s1 = new frmEditStu();
s1.txtSid.Enabled = false;
s1.Show();
}
}
8.学生信息修改窗体。当在学生信息管理窗体上点击“修改”按钮时,能弹出修改学生信息窗体,该窗体够修改学生信息。
按钮点击事件:
[Visual Basic .NET] 纯文本查看 复制代码 private void btnEdit_Click(object sender, EventArgs e)
{
string strSql = "";
if (txtSname.Text != "" && txtBirthday.Text != "" && txtClass.Text != "" && txtTel.Text != "" && txtAddress.Text != "")
{
strSql = "update Studentinfo set Sname='" + txtSname.Text + "',Sex='" + cboSex.Text + "',Birthday='" + txtBirthday.Text + "',Class='" + txtClass.Text + "',Tel='" + txtTel.Text + "',Address='" + txtAddress.Text + "' where Sid='" + frmManageStu.sid + "'";
DataAccess data = new DataAccess();
data.dataCon();
if (data.sqlExec(strSql))
{
MessageBox.Show("修改成功!!");
}
else
{
MessageBox.Show("修改失败!!");
}
}
else
{
MessageBox.Show("输入未完全!!");
}
}
private void frmEditStu_Load(object sender, EventArgs e)
{
txtSid.Text = frmManageStu.sid;
}
private void btnCancle_Click(object sender, EventArgs e)
{
this.Close();
}
9.课程信息添加窗体。能够将课程信息添加到数据库中。
按钮点击事件:
[Visual Basic .NET] 纯文本查看 复制代码 private void btnAdd_Click(object sender, EventArgs e)
{
string strSql;
DataAccess data = new DataAccess();
strSql = "insert into Courseinfo (Cid,Cname,Credit) values ('" + txtCid.Text + "','" + txtCname.Text + "','" + txtCredit.Text + "')";
data.dataCon();
if (data.sqlExec(strSql))
{
MessageBox.Show("添加成功!!");
}
else
{
MessageBox.Show("添加失败!!");
}
}
private void btnCancle_Click(object sender, EventArgs e)
{
this.Close();
}
10.课程信息管理窗体。能够“查询”、“删除”及“修改”课程信息。
按钮点击事件:
[Visual Basic .NET] 纯文本查看 复制代码 public static string cid;
public static string Cid
{
get { return cid; }
set { cid = value; }
}
private void btnSeach_Click(object sender, EventArgs e)
{
string strSql;
string condition = "";
DataAccess data = new DataAccess();
DataSet ds;
if (txtCid.Text != "")
{
condition += "and Cid='" + txtCid.Text + "'";
}
if (txtCname.Text != "")
{
condition += "and Cname='" + txtCname.Text + "'";
}
strSql = "select Cid as 课程编号,Cname as 课程名称,Credit as 学分 from Courseinfo where 1=1" + condition;
data.dataCon();
ds = data.getDataSet(strSql);
dgvInfo.DataSource = ds.Tables[0];
}
private void btnDel_Click(object sender, EventArgs e)
{
if (dgvInfo.CurrentRow == null)
{
MessageBox.Show("请先查询!!");
}
else
{
string strSql;
DataAccess data = new DataAccess();
strSql = "delete from Courseinfo where Cid='" + dgvInfo.CurrentRow.Cells[0].Value.ToString()+"'";
data.dataCon();
if (data.sqlExec(strSql))
{
string Strsql;
DataAccess data1 = new DataAccess();
DataSet ds;
Strsql = "select Cid as 课程编号,Cname as 课程名称,Credit as 学分 from Courseinfo where 1=1";
data1.dataCon();
ds = data1.getDataSet(Strsql);
dgvInfo.DataSource = ds.Tables[0];
MessageBox.Show("删除成功!!");
}
else
{
MessageBox.Show("删除失败!!");
}
}
}
private void btnEdit_Click(object sender, EventArgs e)
{
if (dgvInfo.CurrentRow == null)
{
MessageBox.Show("请先查询!!");
}
else
{
frmManageCourse.cid = dgvInfo.CurrentRow.Cells[0].Value.ToString();
frmEditCourse s1 = new frmEditCourse();
s1.txtCid.Enabled = false;
s1.Show();
}
}
11.课程信息修改窗体。当在课程信息管理窗体上点击“修改”按钮时,能够弹出修改课程信息窗体,该窗体能够修改课程信息。
按钮点击事件:
[Visual Basic .NET] 纯文本查看 复制代码 private void btnEdit_Click(object sender, EventArgs e)
{
string strSql = "";
if (txtCid.Text != "" && txtCname.Text != "" && txtCredit.Text != "")
{
strSql = "update Courseinfo set Cname='" + txtCname.Text + "',Credit='" + txtCredit.Text + "' where Cid='" + frmManageCourse.cid + "'";
DataAccess data = new DataAccess();
data.dataCon();
if (data.sqlExec(strSql))
{
MessageBox.Show("修改成功!!");
}
else
{
MessageBox.Show("修改失败!!");
}
}
else
{
MessageBox.Show("输入未完全!!");
}
}
private void btnCancle_Click(object sender, EventArgs e)
{
this.Close();
}
private void frmEditCourse_Load(object sender, EventArgs e)
{
txtCid.Text = frmManageCourse.cid;
}
12.成绩添加窗体。能够添加学生相应课程的成绩到数据库中。
按钮点击事件:
[Visual Basic .NET] 纯文本查看 复制代码 private void btnAdd_Click(object sender, EventArgs e)
{
string strSql;
DataAccess data = new DataAccess();
strSql = "insert into Scoreinfo (Sid,Cid,Score) values ('" + txtSid.Text + "','" + txtCid.Text + "','" + txtScore.Text + "')";
data.dataCon();
if (data.sqlExec(strSql))
{
MessageBox.Show("添加成功!!");
}
else
{
MessageBox.Show("添加失败!!");
}
}
private void btnCancle_Click(object sender, EventArgs e)
{
this.Close();
}
13.成绩管理窗体。能够“查询”、“删除”及“修改”学生的成绩。
按钮点击事件:
[Visual Basic .NET] 纯文本查看 复制代码 public frmManageScore()
{
InitializeComponent();
check();
}
public void check()
{
string username = FormLogin.Uname;
string password = FormLogin.Upsw;
DataAccess data1 = new DataAccess();
data1.dataCon();
string cmdStr = "Select * from Userinfo where Userid='" + username + "' and Userpwd='" + password + "'";
DataSet ds1;
ds1 = data1.getDataSet(cmdStr);
if (ds1.Tables[0].Rows[0]["Userlevel"].ToString() == "学生")
{
string strSql;
DataAccess data = new DataAccess();
DataSet ds;
strSql = "select Sid as 学号,Sname as 姓名,Sex as 性别,Birthday as 出生日期,Class as 班级,Tel as 电话,Address as 家庭地址 from Studentinfo where Sid='" + username + "'";
data.dataCon();
ds = data.getDataSet(strSql);
txtSid.Text = ds.Tables[0].Rows[0][0].ToString();
txtSid.Enabled = false;
txtSname.Text = ds.Tables[0].Rows[0][1].ToString();
txtSname.Enabled = false;
txtClass.Text = ds.Tables[0].Rows[0][4].ToString();
txtClass.Enabled = false;
}
}
private void button1_Click(object sender, EventArgs e)
{
string username = FormLogin.Uname;
string password = FormLogin.Upsw;
DataAccess data1 = new DataAccess();
data1.dataCon();
string cmdStr = "Select * from Userinfo where Userid='" + username + "' and Userpwd='" + password + "'";
DataSet ds1;
ds1 = data1.getDataSet(cmdStr);
if (ds1.Tables[0].Rows[0]["Userlevel"].ToString() == "学生")
{
string strSql;
string condition = "";
DataAccess data = new DataAccess();
DataSet ds;
if (txtCid.Text != "")
{
if (txtCid.Text == "all")
{
strSql = " SELECT Courseinfo.Cid as 课程号, Courseinfo.Cname as 课程名称, Studentinfo.Sname as 姓名, Studentinfo.Class as 班级, Studentinfo.Sid as 学号, Scoreinfo.Score as 成绩 FROM Studentinfo ,Courseinfo , Scoreinfo where Courseinfo.Cid = Scoreinfo.Cid and Studentinfo.Sid = Scoreinfo.Sid and Studentinfo.Sid='" + txtSid.Text + "' ";
data.dataCon();
ds = data.getDataSet(strSql);
dgvInfo.DataSource = ds.Tables[0];
}
else
{
MessageBox.Show("输入all查询所有成绩");
condition += "and Courseinfo.Cid='" + txtCid.Text + "'";
strSql = " SELECT Courseinfo.Cid as 课程号, Courseinfo.Cname as 课程名称, Studentinfo.Sname as 姓名, Studentinfo.Class as 班级, Studentinfo.Sid as 学号, Scoreinfo.Score as 成绩 FROM Studentinfo ,Courseinfo , Scoreinfo where Courseinfo.Cid = Scoreinfo.Cid and Studentinfo.Sid = Scoreinfo.Sid and Studentinfo.Sid='" + txtSid.Text + "' " + condition;
data.dataCon();
ds = data.getDataSet(strSql);
dgvInfo.DataSource = ds.Tables[0];
}
}
else
{
MessageBox.Show("请输入要查的课程编号!!");
}
}
else
{
string strSql;
string condition = "";
DataAccess data = new DataAccess();
DataSet ds;
if (txtSid.Text != "")
{
condition += "and Studentinfo.Sid='" + txtSid.Text + "'";
}
if (txtSname.Text != "")
{
condition += "and Sname='" + txtSname.Text + "'";
}
if (txtClass.Text != "")
{
condition += "and Class='" + txtClass.Text + "'";
}
if (txtCid.Text != "")
{
condition += "and Courseinfo.Cid='" + txtCid.Text + "'";
}
strSql = " SELECT Courseinfo.Cid as 课程号, Courseinfo.Cname as 课程名称, Studentinfo.Sname as 姓名, Studentinfo.Class as 班级, Studentinfo.Sid as 学号, Scoreinfo.Score as 成绩 FROM Studentinfo ,Courseinfo , Scoreinfo where Courseinfo.Cid = Scoreinfo.Cid and Studentinfo.Sid = Scoreinfo.Sid " + condition;
data.dataCon();
ds = data.getDataSet(strSql);
dgvInfo.DataSource = ds.Tables[0];
}
}
private void btnEdit_Click(object sender, EventArgs e)
{
if (dgvInfo.CurrentRow == null)
{
MessageBox.Show("请先查询!!");
}
else
{
string strSql;
DataAccess data = new DataAccess();
strSql = " update Scoreinfo set Score='" + dgvInfo.CurrentRow.Cells[5].Value.ToString() + "' where Sid='" + dgvInfo.CurrentRow.Cells[4].Value.ToString() + "'and Cid='" + dgvInfo.CurrentRow.Cells[0].Value.ToString() + "'";
data.dataCon();
if (data.sqlExec(strSql))
{
MessageBox.Show("修改成功!!");
}
else
{
MessageBox.Show("修改失败!!");
}
}
}
private void btnDel_Click(object sender, EventArgs e)
{
if (dgvInfo.CurrentRow == null)
{
MessageBox.Show("请先查询!!");
}
else
{
string strSql;
DataAccess data = new DataAccess();
strSql = "delete from Scoreinfo where Cid='" + dgvInfo.CurrentRow.Cells[0].Value.ToString() + "' and Sid='" + dgvInfo.CurrentRow.Cells[4].Value.ToString() + "'";
data.dataCon();
if (data.sqlExec(strSql))
{
string Strsql;
DataAccess data1 = new DataAccess();
DataSet ds;
Strsql = " SELECT Courseinfo.Cid as 课程号, Courseinfo.Cname as 课程名称, Studentinfo.Sname as 姓名, Studentinfo.Class as 班级, Studentinfo.Sid as 学号, Scoreinfo.Score as 成绩 FROM Studentinfo ,Courseinfo , Scoreinfo where Courseinfo.Cid = Scoreinfo.Cid and Studentinfo.Sid = Scoreinfo.Sid ";
data1.dataCon();
ds = data1.getDataSet(Strsql);
dgvInfo.DataSource = ds.Tables[0];
MessageBox.Show("删除成功!!");
}
else
{
MessageBox.Show("删除失败!!");
}
}
}
14.注册窗口
按钮点击事件: [Visual Basic .NET] 纯文本查看 复制代码 public FormRegist()
{
InitializeComponent();
data();
}
public void data()
{
string strSql;
DataAccess data = new DataAccess();
data.dataCon();
strSql = "select Sid from Studentinfo";
DataSet ds;
ds = data.getDataSet(strSql);
if (ds.Tables[0].Rows.Count>0)
{
for (int i = 0; i <ds.Tables[0].Rows.Count; i++)
{
txtUsername.Items.Add(ds.Tables[0].Rows[i][0].ToString());
}
}
}
private void btnRegist_Click(object sender, EventArgs e)
{
string Username = txtUsername.Text.Trim();
string UserPsw = txtPsw.Text.Trim();
string UserRPsw = txtRPsw.Text.Trim();
string UserLevel = cboLevel.Text.Trim();
string StrSql;
DataAccess data0 = new DataAccess();
data0.dataCon();
StrSql = "select Userid from Userinfo where Userid='" + Username + "'";
DataSet ds0;
ds0 = data0.getDataSet(StrSql);
if (ds0.Tables[0].Rows.Count == 1)
{
MessageBox.Show("该账号已注册,请重新输入!!");
txtUsername.Text = "";
txtUsername.Focus();
}
else
{
if (Username == "")
{
MessageBox.Show("用户名不能为空!!");
}
else
{
if (UserPsw == "")
{
MessageBox.Show("密码不能为空!!");
}
else
{
if (UserPsw != UserRPsw)
{
MessageBox.Show("输入密码不一致!!");
}
else
{
if (cboLevel.Text == "学生")
{
string strSql;
DataAccess data = new DataAccess();
strSql = "insert into Userinfo (Userid,Userpwd,Userlevel) values ('" + Username + "','" + UserPsw + "','" + UserLevel + "')";
data.dataCon();
DialogResult Result = MessageBox.Show("信息无误确定注册", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (Result == DialogResult.Yes)
{
string Strsql;
DataAccess data1 = new DataAccess();
data1.dataCon();
Strsql = "select Sid from Studentinfo where Sid='" + Username + "'";
DataSet ds;
ds = data.getDataSet(Strsql);
if (ds.Tables[0].Rows.Count == 1)
{
if (data.sqlExec(strSql))
{
MessageBox.Show("成功注册!");
this.Close();
}
else
{
MessageBox.Show("注册失败!");
}
}
else
{
MessageBox.Show("未查找到此学号,请重新输入!!");
txtUsername.Text = "";
txtUsername.Focus();
}
}
}
}
if (cboLevel.Text == "教师")
{
string strSql;
DataAccess data = new DataAccess();
strSql = "insert into Userinfo (Userid,Userpwd,Userlevel) values ('" + Username + "','" + UserPsw + "','" + UserLevel + "')";
data.dataCon();
DialogResult Result = MessageBox.Show("信息无误确定注册", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (Result == DialogResult.Yes)
{
if (data.sqlExec(strSql))
{
MessageBox.Show("成功注册!");
this.Close();
}
else
{
MessageBox.Show("注册失败!");
}
}
}
}
}
}
}
private void btnCancle_Click(object sender, EventArgs e)
{
this.Close();
}
private void FormRegist_Load(object sender, EventArgs e)
{
cboLevel.SelectedIndex =0;
}
15.改密窗口
按钮点击事件:
[Visual Basic .NET] 纯文本查看 复制代码 private void PswChange_Load(object sender, EventArgs e)
{
txtUsername.Text = FormLogin.Uname;
}
private void btnchange_Click(object sender, EventArgs e)
{
string username = FormLogin.Uname;
string password = FormLogin.Upsw;
string strSql = "";
DataAccess data = new DataAccess();
data.dataCon();
string cmdStr = "Select * from Userinfo where Userid='" + username + "' and Userpwd='" + password + "'";
DataSet ds;
ds = data.getDataSet(cmdStr);
if (txtPassword.Text==password)
{
strSql = "update Userinfo set Userpwd='" + txtNewPsd.Text.Trim()+ "' where Userid='" + username + "'";
DataAccess data1 = new DataAccess();
data1.dataCon();
if (data1.sqlExec(strSql))
{
MessageBox.Show("修改密码成功!!");
this.Close();
}
else
{
MessageBox.Show("修改密码失败!!");
}
}
else
{
MessageBox.Show("原密码不正确!!");
}
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
} 相关资源下载请移步数据库设计。。。
|