📄 score.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace StudentManager
{
public partial class Score : Form
{
private string selectstr = null;
private string commandstr = null;
private SqlConnection conn1 = null;
private SqlDataAdapter DataAdepter = null;
private DataSet Dataset = new DataSet();
private SqlCommand Command1 = null;
private SqlDataReader DataReader = null;
public Score()
{
InitializeComponent();
conn1 = new SqlConnection(DataLevel.Connection.ConnString);
this.selectstr = "Select * from selectCourse";
this.Command1 = new SqlCommand();
this.Command1.CommandText = this.selectstr;
this.Command1.Connection = this.conn1;
}
private void Score_Load(object sender, EventArgs e)
{
Combo_Fill();
}
private void Combo_Fill()
{
if (this.conn1.State == ConnectionState.Closed)
this.conn1.Open();
this.commandstr = "Select DepartmentName from Department";
this.Command1.CommandText = this.commandstr;
this.Command1.Connection = conn1;
this.DataReader = this.Command1.ExecuteReader();
while (DataReader.Read())
{
this.comboBoxDepartment.Items.Add(this.DataReader["DepartmentName"].ToString());
}
this.DataReader.Close();
this.commandstr = "Select SemesterName from Semester";
this.Command1.CommandText = this.commandstr;
this.DataReader = this.Command1.ExecuteReader();
while (DataReader.Read())
{
this.comboBoxSemester.Items.Add(this.DataReader["SemesterName"].ToString());
}
this.DataReader.Close();
this.conn1.Close();
}
private void comboBoxDepartment_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.conn1.State == ConnectionState.Closed)
conn1.Open();
this.comboBoxClass.Items.Clear();
this.commandstr = "Select ClassName from Class where DepartmentName = '" + this.comboBoxDepartment.Text + "'";
this.Command1.CommandText = this.commandstr;
this.DataReader = this.Command1.ExecuteReader();
while (DataReader.Read())
{
this.comboBoxClass.Items.Add(this.DataReader["ClassName"].ToString());
}
this.DataReader.Close();
this.conn1.Close();
}
private void btnApply_Click(object sender, EventArgs e)
{
this.comboBoxCourse.Items.Clear();
if (this.conn1.State == ConnectionState.Closed)
conn1.Open();
this.commandstr = "Select * from SelectCourse where Department='" + this.comboBoxDepartment.Text + "'and Class='" + this.comboBoxClass.Text + "'and Semester='" + this.comboBoxSemester.Text + "'";
// this.DataAdepter = new SqlDataAdapter(this.selectstr, this.conn1);
// this.DataAdepter.Fill(this.Dataset, "SelectCourse");
this.Command1.CommandText = this.commandstr;
this.Command1.Connection = this.conn1;
this.DataReader = this.Command1.ExecuteReader();
while (DataReader.Read())
{
this.comboBoxCourse.Items.Add(DataReader["Course"].ToString());
}
this.DataReader.Close();
this.conn1.Close();
}
private void btnApply2_Click(object sender, EventArgs e)
{
if (this.conn1.State == ConnectionState.Closed)
conn1.Open();
this.selectstr = "select * from Score where Class='" + this.comboBoxClass.Text + "'";
this.DataAdepter = new SqlDataAdapter(this.selectstr, this.conn1);
this.Dataset.Clear();
this.DataAdepter.Fill(this.Dataset, "Score");
if (this.Dataset.Tables[0].Rows.Count == 0)
{
commandstr = "Select StudentID from Student where class = '" + this.comboBoxClass.Text + "'";
Command1.CommandText = this.commandstr;
this.DataReader=Command1.ExecuteReader();
while (DataReader.Read())
{
DataRow newrow;
newrow = this.Dataset.Tables[0].NewRow();
newrow["StudentID"] = this.DataReader[0].ToString();
newrow["Class"] = this.comboBoxClass.Text;
newrow["Course"] = this.comboBoxCourse.Text;
newrow["Score"] = 0;
this.Dataset.Tables[0].Rows.Add(newrow);
}
this.DataReader.Close();
}
this.conn1.Close();
this.dataGridView1.DataSource = this.Dataset;
this.dataGridView1.DataMember ="Score";
/* this.selectstr = "select * from Score where Class='" + this.comboBoxClass.Text + "'";
this.DataAdepter = new SqlDataAdapter(this.selectstr, this.conn1);
this.Dataset.Clear();
this.DataAdepter.Fill(this.Dataset, "Score");
if (this.Dataset.Tables[0].Rows.Count == 0)
{
try
{
if (this.conn1.State == ConnectionState.Closed) this.conn1.Open();
this.commandstr = "select StudentID from Student where Class='" + this.comboBoxClass.Text + "'";
this.Command1.CommandText = this.commandstr;
this.DataReader = this.Command1.ExecuteReader();
while (this.DataReader.Read())
{
DataRow newrow;
newrow = this.Dataset.Tables[0].NewRow();
newrow["StudentID"] = DataReader[0].ToString();
newrow["Class"] = this.comboBoxClass.Text;
newrow["Course"] = this.comboBoxCourse.Text;
newrow["Score"] = 0;
this.Dataset.Tables[0].Rows.Add(newrow);
}
this.DataReader.Close();
}
catch (Exception E)
{
MessageBox.Show(E.ToString());
}
finally
{
this.conn1.Close();
}
}
this.dataGridView1.DataSource = this.Dataset;
this.dataGridView1.DataMember = "Score"; */
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnOK_Click(object sender, EventArgs e)
{
this.BindingContext[this.Dataset, "Score"].EndCurrentEdit();
if (this.conn1.State == ConnectionState.Closed)
this.conn1.Open();
SqlCommandBuilder Commanderbuiter = new SqlCommandBuilder(this.DataAdepter);
this.DataAdepter.Update(this.Dataset, "Score");
this.Dataset.AcceptChanges();
this.dataGridView1.Refresh();
this.conn1.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -