因为上个界面实在是太卡了,尤其在c#代码里,打字都是延缓的,所以分开来展示。
这一个主要是管理员界面的介绍,窗口调用如下:
ManageMain窗口:(管理员主界面)省略代码和前文一样,
(学生管理)
(课程管理)
(成绩管理)
(登录日志查询)
代码展示:
public partial class ManageMain : Form
{
public ManageMain()
{
InitializeComponent();
}
//这个重绘控件和StudentMain 相同,可忽略
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
//重绘控件
{ //使选项卡文字水平
SolidBrush _Brush = new SolidBrush(Color.Black);//单色画刷
RectangleF _TabTextArea = (RectangleF)tabControl1.GetTabRect(e.Index);//绘制区域
StringFormat _sf = new StringFormat();//封装文本布局格式信息
_sf.LineAlignment = StringAlignment.Center;
_sf.Alignment = StringAlignment.Center;
e.Graphics.DrawString(tabControl1.Controls[e.Index].Text, SystemInformation.MenuFont, _Brush, _TabTextArea, _sf);
//填充背景颜色
//标签背景填充颜色
SolidBrush BackBrush = new SolidBrush(Color.FromArgb(16, 24, 80));
//标签文字填充颜色
SolidBrush FrontBrush = new SolidBrush(Color.FromArgb(210, 210, 221));
StringFormat StringF = new StringFormat();
//设置文字对齐方式
StringF.Alignment = StringAlignment.Center;
StringF.LineAlignment = StringAlignment.Center;
for (int i = 0; i < tabControl1.TabPages.Count; i++)
{
//获取标签头工作区域
Rectangle Rec = tabControl1.GetTabRect(i);
e.Graphics.FillRectangle(BackBrush, Rec);
//绘制标签头文字
e.Graphics.DrawString(tabControl1.TabPages[i].Text, new Font("楷体", 10), FrontBrush, Rec, StringF);
}
}
private void ManageMain_Load(object sender, EventArgs e)
{
this.sysLogTableAdapter.Fill(this.curricula_variable_systemDataSet11.SysLog);
//this.courseTableAdapter1.Fill(this.curricula_variable_systemDataSet9.Course);
this.courseTableAdapter.Fill(this.curricula_variable_systemDataSet8.Course);
//this.studentTableAdapter.Fill(this.curricula_variable_systemDataSet7.Student);
string myConnString = "Data Source=.;Initial Catalog=curricula_variable_system;Persist Security Info=True;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(myConnString); //实例化连接对象
sqlConnection.Open(); //数据库打开
string sql;
sql = "select UserPhoto from SysManage where UserID = '" + Login.getusername() + "'";
SqlDataAdapter da2 = new SqlDataAdapter(sql, sqlConnection);
DataTable dt2 = new DataTable();
da2.Fill(dt2);
//显示用户图片
MemoryStream ms = new MemoryStream((byte[])dt2.Rows[0][0]);
Bitmap bmpt = new Bitmap(ms);
this.pictureBox1.Image = bmpt;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
//点击查询学生信息
private void button4_Click(object sender, EventArgs e)
{
//查询方式
string way = comboBox1.Text.Trim();
//输入的信息
string infor = textBox1.Text.Trim();
string conn = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(conn);
try
{
sqlConnection.Open();
string sql;
if (way.Equals("按学号查询"))
{
if (infor.Equals(""))
sql = "select * from Student";
else
sql = "select * from Student where Sno like '" + infor + "%'";
}
else if (way.Equals("按姓名查询"))
{
if (infor.Equals(""))
sql = "select * from Student";
else
sql = "select * from Student where Sname like '" + infor + "%'";
}
else
{
sql = "select * from Student where Sname like '" + infor + "%' or Sno like '" + infor + "%'";
}
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource(); //绑定
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
m = new MessageBox();
timer1.Enabled = true;
m.show("程序发生错误!");
}
finally
{
sqlConnection.Close();
}
}
//添加学生信息按钮
private void button1_Click(object sender, EventArgs e)
{
//打开添加学生新窗口
AddStu form = new AddStu();
form.ShowDialog();
//下面这一大段代码是复制上面查询学生按钮的代码,相当于刷新了一遍(不做展示)
sqlConnection.Close();
}
//点击dategridview1中的按钮
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
string conn = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(conn);
sqlConnection.Open();
//datagirdview中修改按钮的点击事件
if (dataGridView1.Columns[e.ColumnIndex].Name == "修改" && e.RowIndex >= 0)
{
//说明点击的是修改按钮这一列
DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
//打开修改界面新窗体
AlterStu b = new AlterStu(dataGridView1.CurrentRow);
b.ShowDialog();
//刷新(不做展示)
sqlConnection.Close();
}
//按钮删除的点击事件
else if (dataGridView1.Columns[e.ColumnIndex].Name == "删除" && e.RowIndex >= 0)
{
//说明点击的是按钮这一列
DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
DeleteTip a = new DeleteTip(dataGridView1.CurrentRow);
a.ShowDialog();
//刷新一遍(不做展示)
sqlConnection.Close();
}
}
//添加课程信息按钮
private void button3_Click_1(object sender, EventArgs e)
{
AddCourse form = new AddCourse();
form.ShowDialog();
//刷新一遍课程表(不做展示)
}
//查询课程信息按钮按照课程名名字
private void button2_Click(object sender, EventArgs e)
{
//选择的课程信息
string course = comboBox2.Text.Trim();
string conn = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(conn);
sqlConnection.Open();
string sql;
if(course.Equals("全部"))
{
sql = "select * from Course where Cname not like '全部'";
}
else
{
sql = "select * from Course where Cname = '" + course + "'and Cname not like '全部'";
}
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource(); //绑定
bindingSource.DataSource = sqlDataReader;
dataGridView2.DataSource = bindingSource;
sqlConnection.Close();
}
//按课序号查询课程信息
private void button7_Click(object sender, EventArgs e)
{
//选择的课程信息
string cno = textBox4.Text.Trim();
string conn = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(conn);
sqlConnection.Open();
string sql;
if (cno.Equals(""))
{
sql = "select * from Course where Cname not like '全部'";
}
else
{
sql = "select * from Course where Cno = '" + cno + "'and Cname not like '全部'";
}
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource(); //绑定
bindingSource.DataSource = sqlDataReader;
dataGridView2.DataSource = bindingSource;
sqlConnection.Close();
}
//静态变量,为之后的页面提供变量
private static string cno;
public static string getcno()
{
return cno;
}
private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
string course = comboBox2.Text.Trim();
cno = Convert.ToString(dataGridView2.CurrentRow.Cells[0].Value).Trim();
string conn = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(conn);
sqlConnection.Open();
if (dataGridView2.Columns[e.ColumnIndex].Name == "修改2" && e.RowIndex >= 0)
{
//说明点击的是修改按钮这一列
DataGridViewColumn column = dataGridView2.Columns[e.ColumnIndex];
AlterCourse b = new AlterCourse(dataGridView2.CurrentRow);
b.ShowDialog();
//刷新一下(不予展示)
}
else if (dataGridView2.Columns[e.ColumnIndex].Name == "删除2" && e.RowIndex >= 0)
{
//点击的是删除这一列
DataGridViewColumn column = dataGridView2.Columns[e.ColumnIndex];
DeleteTip2 a = new DeleteTip2(dataGridView2.CurrentRow);
a.ShowDialog();
string sql;
//刷新一遍,(不做展示)
}
else if (dataGridView2.Columns[e.ColumnIndex].Name == "详细" && e.RowIndex >= 0)
{
//点击的是详细这一列
DataGridViewColumn column = dataGridView2.Columns[e.ColumnIndex];
//这一行传给了DetailedCourse,再由DetailedCourse传给AlterDeC,用于添加详细课程时使用课程号啥的
DetailedCourse form = new DetailedCourse(dataGridView2.CurrentRow);
form.ShowDialog();
}
sqlConnection.Close();
}
private void ManageMain_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
//点击查询信息
private void button5_Click(object sender, EventArgs e)
{
//选择的课程信息
string course = comboBox3.Text.Trim();
string snoorsname = textBox2.Text.Trim();
string conn = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(conn);
sqlConnection.Open();
string sql;
//课程选的全部并且学号没有填写信息
if (course.Equals("全部")&& snoorsname.Equals(""))
{
sql = "select distinct Sno,Cno,Cname,Grade from SC";
}
else
{
//为了有一个全部选项,我在课程表里填了一个全部,不能显示它
if(snoorsname.Equals(""))
sql = "select distinct Sno,Cno,Cname,Grade from SC where Cname = '" + course + "'";
else
{
if (course.Equals("全部"))
sql = "select distinct Sno,Cno,Cname,Grade from SC where Sno like '" + snoorsname + "%' or Sno in (select Sno from Student where Sname like '" + snoorsname + "%')";
else
sql = "select distinct Sno,Cno,Cname,Grade from SC where Cname = '" + course + "' and (Sno like '" + snoorsname + "%' or Sno in (select Sno from Student where Sname like '" + snoorsname + "%'))";
}
}
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource(); //绑定
bindingSource.DataSource = sqlDataReader;
dataGridView4.DataSource = bindingSource;
sqlConnection.Close();
}
//点击修改
private void button6_Click(object sender, EventArgs e)
{
//要修改的成绩
string grade = textBox3.Text.Trim();
//选中行的学号和课序号
string sno = Convert.ToString(dataGridView4.CurrentRow.Cells[0].Value).Trim();
string cno = Convert.ToString(dataGridView4.CurrentRow.Cells[1].Value).Trim();
string conn = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(conn);
sqlConnection.Open();
string sql = "update SC set Grade = " + grade + " where Sno = '" + sno + "' and Cno = '" + cno + "'";
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
sqlDataReader.Close();
//刷新一下,和上面查询一摸一样(不做展示)
sqlConnection.Close();
}
//点击下三角
private void button12_Click(object sender, EventArgs e)
{
if (this.panel2.Visible == false)
this.panel2.Visible = true;
else
this.panel2.Visible = false;
}
//点击修改个人信息
private void button13_Click(object sender, EventArgs e)
{
AlterInforMan a = new AlterInforMan();
a.ShowDialog();
string myConnString = "Data Source=.;Initial Catalog=curricula_variable_system;Persist Security Info=True;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(myConnString); //实例化连接对象
sqlConnection.Open(); //数据库打开
string sql;
sql = "select UserPhoto from SysManage where UserID = '" + Login.getusername() + "'";
SqlDataAdapter da2 = new SqlDataAdapter(sql, sqlConnection);
DataTable dt2 = new DataTable();
da2.Fill(dt2);
MemoryStream ms = new MemoryStream((byte[])dt2.Rows[0][0]);
Bitmap bmpt = new Bitmap(ms);
this.pictureBox1.Image = bmpt;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
//点击退出登录
private void button14_Click(object sender, EventArgs e)
{
Application.Exit();//结束进程
}
}
【学生管理】调用窗口
学生1:添加学生 AddStu窗口:
代码:
public partial class AddStu : Form
{
public AddStu()
{
InitializeComponent();
}
//添加学生信息按钮
private void button1_Click(object sender, EventArgs e)
{
//提取填入学生信息
string sno = textBox1.Text.Trim();
string sname = textBox2.Text.Trim();
string sex = comboBox1.Text.Trim();
string sage = textBox4.Text.Trim();
string sdept = textBox5.Text.Trim();
//数据库连接
string conn = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(conn);
try
{
sqlConnection.Open();
//查询要填入的信息学号是不是存在
string sql = "select * from Student where sno = '" + sno + "'";
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
//如果这个学号已经存在了
if(sqlDataReader.HasRows)
{
m = new MessageBox();
timer1.Enabled = true;
m.show("无法添加,要添加的学号已经存在!");
return;
}
//开始添加
sqlDataReader.Close();
sql = "insert into Student values('" + sno + "','" + sname + "','" + sex + "','" + sage + "','" + sdept + "')";
sqlCommand = new SqlCommand(sql, sqlConnection);
sqlDataReader = sqlCommand.ExecuteReader();
m = new MessageBox();
timer1.Enabled = true;
m.show("添加成功!");
}
catch
{
m = new MessageBox();
timer1.Enabled = true;
m.show("程序出现问题!");
}
finally
{
sqlConnection.Close();
this.Close();
}
}
}
学生2:修改学生 AlterStu窗口:
代码
public partial class AlterStu : Form
{
public AlterStu()
{
InitializeComponent();
}
//新的构造函数,用于传进一行
DataGridViewRow dgvrow;
public AlterStu(DataGridViewRow dgv)
{
dgvrow = dgv;
InitializeComponent();
}
private void AlterStu_Load(object sender, EventArgs e)
{
textBox1.Text= dgvrow.Cells[0].Value.ToString();
textBox2.Text= dgvrow.Cells[1].Value.ToString();
comboBox1.Text= dgvrow.Cells[2].Value.ToString();
textBox4.Text= dgvrow.Cells[3].Value.ToString();
textBox5.Text= dgvrow.Cells[4].Value.ToString();
textBox1.Enabled = false;
}
//点击确定按钮
private void button1_Click(object sender, EventArgs e)
{
//提取填入学生信息
string sno = textBox1.Text.Trim();
string sname = textBox2.Text.Trim();
string sex = comboBox1.Text.Trim();
string sage = textBox4.Text.Trim();
string sdept = textBox5.Text.Trim();
//数据库连接
string conn = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(conn);
sqlConnection.Open();
if(sno != dgvrow.Cells[0].Value.ToString())
{
m = new MessageBox();
timer1.Enabled = true;
m.show("学号不允许修改!");
return;
}
//开始修改
string sql = "update Student set Sname = '" + sname + "',Ssex='" + sex + "' ,Sage='" + sage + "',Sdept='" + sdept + "' where Sno = '" + dgvrow.Cells[0].Value.ToString()+"'";
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
m = new MessageBox();
timer1.Enabled = true;
m.show("修改成功!");
this.Close();
}
}
学生3:删除提示 DeleteTip:
public partial class DeleteTip : Form
{
public DeleteTip()
{
InitializeComponent();
}
DataGridViewRow dgvrow;
public DeleteTip(DataGridViewRow dgv)
{
dgvrow = dgv;
InitializeComponent();
}
private void DeleteTip_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“curricula_variable_systemDataSet13.SC”中。您可以根据需要移动或删除它。
//this.sCTableAdapter.Fill(this.curricula_variable_systemDataSet13.SC);
string sno = dgvrow.Cells[0].Value.ToString();
string conn = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(conn);
sqlConnection.Open();
string sql = "select * from SC where Sno = '" + sno + "'";
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource(); //绑定
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
//点击删除
private void button1_Click(object sender, EventArgs e)
{
string sno = dgvrow.Cells[0].Value.ToString();
string conn = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(conn);
sqlConnection.Open();
//有关的全部删除
string sql = "delete from SC where Sno = '" + sno + "'";
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
sqlDataReader.Close();
sql = "delete from Student where Sno = '" + sno + "'";
sqlCommand = new SqlCommand(sql, sqlConnection);
sqlDataReader = sqlCommand.ExecuteReader();
sqlDataReader.Close();
sql = "update SysUser set UserBindingID = null where UserBindingId = '" + sno + "'";
sqlCommand = new SqlCommand(sql, sqlConnection);
sqlDataReader = sqlCommand.ExecuteReader();
this.Close();
}
//点击取消
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
//合并单元格
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == 0 && e.RowIndex != -1 ||e.ColumnIndex == 1 && e.RowIndex != -1 || e.ColumnIndex == 2 && e.RowIndex != -1)
{
Brush bruch = new SolidBrush(dataGridView1.GridColor);
SolidBrush sbruch = new SolidBrush(e.CellStyle.BackColor);
using (Pen pen = new Pen(bruch))
{
e.Graphics.FillRectangle(sbruch, e.CellBounds);
if (e.RowIndex < dataGridView1.Rows.Count - 1 && dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value != null && dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString() != e.Value.ToString())
{
e.Graphics.DrawLine(pen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1);
e.Graphics.DrawLine(pen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);
}
else
{
e.Graphics.DrawLine(pen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);
}
if (e.RowIndex == dataGridView1.Rows.Count - 1)
{
e.Graphics.DrawLine(pen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1);
}
if (e.Value != null)
{
if (!(e.RowIndex > 0 && dataGridView1.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString() == e.Value.ToString()))
{
e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 2, e.CellBounds.Y + 5, StringFormat.GenericDefault);
}
}
e.Handled = true;
}
}
}
}
【课程管理】调用窗口
课程1:添加课程 AddCourse:
public partial class AddCourse : Form
{
public AddCourse()
{
InitializeComponent();
}
//课程添加窗口的确定按钮
private void button1_Click(object sender, EventArgs e)
{
string cno = textBox1.Text.Trim();
string cname = textBox2.Text.Trim();
string credit = textBox3.Text.Trim();
if(cno=="" || cname =="" || credit =="")
{
m = new MessageBox();
timer1.Enabled = true;
m.show("不能填入空数据!");
return;
}
string conn = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(conn);
try
{
sqlConnection.Open();
//查询要填入的信息课程号是不是存在
string sql = "select * from Course where Cno = '" + cno + "'";
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
//如果这个课序号已经存在了
if (sqlDataReader.HasRows)
{
m = new MessageBox();
timer1.Enabled = true;
m.show("无法添加,要添加的课序号已经存在!");
return;
}
//开始添加
sqlDataReader.Close();
sql = "insert into Course values('" + cno + "','" + cname + "','" + credit + "')";
sqlCommand = new SqlCommand(sql, sqlConnection);
sqlDataReader = sqlCommand.ExecuteReader();
m = new MessageBox();
timer1.Enabled = true;
m.show("添加成功!");
}
catch
{
m = new MessageBox();
timer1.Enabled = true;
m.show("程序出现问题!");
}
finally
{
sqlConnection.Close();
this.Close();
}
}
}
课程2:修改课程 AlterCourse:
public partial class AlterCourse : Form
{
public AlterCourse()
{
InitializeComponent();
}
//新的构造函数,用于传进一行
DataGridViewRow dgvrow;
public AlterCourse(DataGridViewRow dgv)
{
dgvrow = dgv;
InitializeComponent();
}
string pcno;
private void AlterCourse_Load(object sender, EventArgs e)
{
textBox1.Text = dgvrow.Cells[0].Value.ToString();
//未改动前的课序号
pcno = textBox1.Text.Trim();
textBox2.Text = dgvrow.Cells[1].Value.ToString();
textBox3.Text = dgvrow.Cells[2].Value.ToString();
textBox1.Enabled = false;
}
private void button1_Click(object sender, EventArgs e)
{
//获得修改后的数据
string cno = textBox1.Text.Trim();
string cname = textBox2.Text.Trim();
string credit = textBox3.Text.Trim();
string conn = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(conn);
sqlConnection.Open();
if( cno!=pcno)
{
m = new MessageBox();
timer1.Enabled = true;
m.show("课序号不允许修改!");
return;
}
string up = "update Course set Cname ='" + cname + "',Ccredit='" + credit + "' where Cno='" + pcno + "'";
SqlCommand sqlCommand2 = new SqlCommand(up, sqlConnection);
SqlDataReader sqlDataReader2 = sqlCommand2.ExecuteReader();
sqlDataReader2.Close();
up = "update DetailedCourse set Cname ='" + cname + "',Ccredit='" + credit + "' where Cno='" + pcno + "'";
sqlCommand2 = new SqlCommand(up, sqlConnection);
sqlDataReader2 = sqlCommand2.ExecuteReader();
m = new MessageBox();
timer1.Enabled = true;
m.show("修改成功!");
this.Close();
}
}
课程3:删除课程提示 DeleteTip2
public partial class DeleteTip2 : Form
{
public DeleteTip2()
{
InitializeComponent();
}
DataGridViewRow dgvrow;
public DeleteTip2(DataGridViewRow dgv)
{
dgvrow = dgv;
InitializeComponent();
}
private void DeleteTip2_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“curricula_variable_systemDataSet17.SC”中。您可以根据需要移动或删除它。
//this.sCTableAdapter.Fill(this.curricula_variable_systemDataSet17.SC);
string cno = dgvrow.Cells[0].Value.ToString();
string conn = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(conn);
sqlConnection.Open();
string sql = "select * from SC where cno = '" + cno + "'";
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource(); //绑定
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
//点击删除
private void button1_Click(object sender, EventArgs e)
{
string cno = dgvrow.Cells[0].Value.ToString();
string conn = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(conn);
sqlConnection.Open();
string sql = "delete from SC where Cno = '" + cno + "'";
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
sqlDataReader.Close();
sql = "delete from Course where Cno = '" + cno + "'";
sqlCommand = new SqlCommand(sql, sqlConnection);
sqlDataReader = sqlCommand.ExecuteReader();
sqlDataReader.Close();
sql = "delete from DetailedCourse where Cno = '" + cno + "'";
sqlCommand = new SqlCommand(sql, sqlConnection);
sqlDataReader = sqlCommand.ExecuteReader();
sqlDataReader.Close();
this.Close();
}
//点击取消
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
课程4:详细课程界面 DetailedCourse
public partial class DetailedCourse : Form
{
public DetailedCourse()
{
InitializeComponent();
}
//一行传来
DataGridViewRow dgvrow;
public DetailedCourse(DataGridViewRow dgv)
{
dgvrow = dgv;
InitializeComponent();
}
private void DetailedCourse_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“curricula_variable_systemDataSet10.DetailedCourse”中。您可以根据需要移动或删除它。
//this.detailedCourseTableAdapter.Fill(this.curricula_variable_systemDataSet10.DetailedCourse);
string conn = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(conn);
sqlConnection.Open();
//把详细课程表中关于这个课程的数据加载出来
string sql = "select * from DetailedCourse where Cno = '" + ManageMain.getcno() + "'";
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource(); //绑定显示信息
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
//添加详细课程表
private void button1_Click(object sender, EventArgs e)
{
AddDe form = new AddDe(dgvrow);
form.ShowDialog();
//刷新一下(不予展示)
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
string select_cno = Convert.ToString(dataGridView1.CurrentRow.Cells[0].Value).Trim();
string select_cname = Convert.ToString(dataGridView1.CurrentRow.Cells[1].Value).Trim();
string select_jie = Convert.ToString(dataGridView1.CurrentRow.Cells[3].Value).Trim();
string select_ke = Convert.ToString(dataGridView1.CurrentRow.Cells[4].Value).Trim();
string select_zhou = Convert.ToString(dataGridView1.CurrentRow.Cells[5].Value).Trim();
string conn = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(conn);
sqlConnection.Open();
if (dataGridView1.Columns[e.ColumnIndex].Name == "修改" && e.RowIndex >= 0)
{
//说明点击的是修改按钮这一列
DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
//打开修改界面新窗体
AlterDetailedC b = new AlterDetailedC(dataGridView1.CurrentRow);
b.ShowDialog();
//刷新页面(不予展示)
}
else if (dataGridView1.Columns[e.ColumnIndex].Name == "删除" && e.RowIndex >= 0)
{
//说明点击的是删除按钮这一列
DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
try
{
string sqll = "delete from DetailedCourse where Cno = '" + select_cno + "' and ClassTime = '" + select_jie + "' and Lessonperiod = '" + select_ke + "' and Weeks = '" + select_zhou + "'";
SqlCommand sqlCommand2 = new SqlCommand(sqll, sqlConnection);
SqlDataReader sqlDataReader2 = sqlCommand2.ExecuteReader();
sqlDataReader2.Close();
}
catch
{
m = new MessageBox();
timer1.Enabled = true;
m.show("该课程已经被学生选中,删除将会导致对于这门课的选课记录消失!");
}
finally
{
string sqll = "delete from SC where Cno = '" + select_cno + "' and ClassTime = '" + select_jie + "' and Lessonperiod = '" + select_ke + "' and Weeks = '" + select_zhou + "'";
SqlCommand sqlCommand2 = new SqlCommand(sqll, sqlConnection);
SqlDataReader sqlDataReader2 = sqlCommand2.ExecuteReader();
sqlDataReader2.Close();
sqll = "delete from DetailedCourse where Cno = '" + select_cno + "' and ClassTime = '" + select_jie + "' and Lessonperiod = '" + select_ke + "' and Weeks = '" + select_zhou + "'";
sqlCommand2 = new SqlCommand(sqll, sqlConnection);
sqlDataReader2 = sqlCommand2.ExecuteReader();
sqlDataReader2.Close();
}
//刷新一下(不予展示)
}
}
}
课程5:详细课程添加 AddDe
public partial class AddDe : Form
{
public AddDe()
{
InitializeComponent();
}
//获取一行
DataGridViewRow dgvrow;
public AddDe(DataGridViewRow dgv)
{
dgvrow = dgv;
InitializeComponent();
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void label4_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string cno = textBox1.Text.Trim();
string cname = textBox2.Text.Trim();
string credit = textBox3.Text.Trim();
string jie = textBox4.Text.Trim();
string ke = textBox5.Text.Trim();
string zhou = textBox6.Text.Trim();
string conn = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(conn);
try
{
sqlConnection.Open();
//查询要填入的详细信息课程的上课事件是不是存在,一门课程上课事件不能产生冲突
string sql = "select * from DetailedCourse where Cno = '" + cno + "' and ClassTime = '" + jie + "' and Lessonperiod = '"+ke+"' and Weeks = '"+zhou+"'";
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
//如果已经存在了
if (sqlDataReader.HasRows)
{
m = new MessageBox();
timer1.Enabled = true;
m.show("无法添加,上课时间与这门课程的其他时间冲突!");
return;
}
//开始添加
sqlDataReader.Close();
sql = "insert into DetailedCourse values('" + cno + "','" + cname + "','" + credit + "','" + jie + "','" + ke + "','" + zhou + "')";
sqlCommand = new SqlCommand(sql, sqlConnection);
sqlDataReader = sqlCommand.ExecuteReader();
m = new MessageBox();
timer1.Enabled = true;
m.show("添加成功!");
}
catch
{
m = new MessageBox();
timer1.Enabled = true;
m.show("程序出现问题!");
}
finally
{
sqlConnection.Close();
this.Close();
}
}
private void AddDe_Load(object sender, EventArgs e)
{
textBox1.Text = dgvrow.Cells[0].Value.ToString().Trim();
textBox2.Text = dgvrow.Cells[1].Value.ToString().Trim();
textBox3.Text = dgvrow.Cells[2].Value.ToString().Trim();
textBox1.Enabled = false;
textBox2.Enabled = false;
textBox3.Enabled = false;
}
}
课程6:详细课程修改 AlterDetailedC
public partial class AlterDetailedC : Form
{
public AlterDetailedC()
{
InitializeComponent();
}
//新的构造函数,用于传进一行
DataGridViewRow dgvrow;
public AlterDetailedC(DataGridViewRow dgv)
{
dgvrow = dgv;
InitializeComponent();
}
private void AlterDetailedC_Load(object sender, EventArgs e)
{
//当表中什么也没有的时候,点击修改,这里会出异常,try也拦不住,不太明白为什么,就忽略了
try
{
textBox1.Text = dgvrow.Cells[0].Value.ToString().Trim();
textBox2.Text = dgvrow.Cells[1].Value.ToString().Trim();
textBox3.Text = dgvrow.Cells[2].Value.ToString().Trim();
textBox4.Text = dgvrow.Cells[3].Value.ToString().Trim();
textBox5.Text = dgvrow.Cells[4].Value.ToString().Trim();
textBox6.Text = dgvrow.Cells[5].Value.ToString().Trim();
}
catch
{
m = new MessageBox();
timer1.Enabled = true;
m.show("不能修改空值!");
return;
}
//不可编辑
textBox1.ReadOnly = true;
textBox2.ReadOnly = true;
textBox3.ReadOnly = true;
}
private void button1_Click(object sender, EventArgs e)
{
string cno = textBox1.Text.Trim();
string cname = textBox2.Text.Trim();
string credit = textBox3.Text.Trim();
string jie = textBox4.Text.Trim();
string ke = textBox5.Text.Trim();
string zhou = textBox6.Text.Trim();
string conn = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(conn);
sqlConnection.Open();
if (cno!= dgvrow.Cells[0].Value.ToString().Trim() || cname!= dgvrow.Cells[1].Value.ToString().Trim() || credit!= dgvrow.Cells[2].Value.ToString().Trim())
{
m = new MessageBox();
timer1.Enabled = true;
m.show("无法修改课序号,课程名和学分!");
return;
}
//查询要填入的详细信息课程的上课事件是不是存在,一门课程上课事件不能产生冲突
string sql = "select * from DetailedCourse where Cno = '" + cno + "' and ClassTime = '" + jie + "' and Lessonperiod = '"+ke+"' and Weeks = '"+zhou+"'";
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
//如果已经存在了
if (sqlDataReader.HasRows)
{
m = new MessageBox();
timer1.Enabled = true;
m.show("无法添加,上课时间与这门课程的其他时间冲突!");
return;
}
//开始添加
sqlDataReader.Close();
//修改语句
sql = "update DetailedCourse set ClassTime = '" + jie + "',Lessonperiod = '" + ke + "', Weeks = '" + zhou + "' where Cno='"+cno+ "' and ClassTime = '" + dgvrow.Cells[3].Value.ToString().Trim() + "' and Lessonperiod = '" + dgvrow.Cells[4].Value.ToString().Trim() + "' and Weeks = '" + dgvrow.Cells[5].Value.ToString().Trim() + "'";
sqlCommand = new SqlCommand(sql, sqlConnection);
sqlDataReader = sqlCommand.ExecuteReader();
m = new MessageBox();
timer1.Enabled = true;
m.show("修改成功!");
sqlConnection.Close();
this.Close();
}
}
视频讲解:我的数据库大作业