📄 grade.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace 学生与成绩
{
/// <summary>
/// Grade 的摘要说明。
/// </summary>
public class Grade : System.Windows.Forms.Form
{
private DataTable myTable;
private string sendTableName="STU";
private string sendStrSQL;
private DataSet ds=new DataSet();
private CurrencyManager cmOrders;
private LinkDataBase link=new LinkDataBase();
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.ComboBox comboBox2;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Grade()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.comboBox2 = new System.Windows.Forms.ComboBox();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(32, 21);
this.label1.Name = "label1";
this.label1.TabIndex = 0;
this.label1.Text = "选择班级";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label2
//
this.label2.Location = new System.Drawing.Point(32, 64);
this.label2.Name = "label2";
this.label2.TabIndex = 1;
this.label2.Text = "选择课程";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// comboBox1
//
this.comboBox1.Location = new System.Drawing.Point(136, 24);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 20);
this.comboBox1.TabIndex = 2;
//
// comboBox2
//
this.comboBox2.Location = new System.Drawing.Point(136, 64);
this.comboBox2.Name = "comboBox2";
this.comboBox2.Size = new System.Drawing.Size(121, 20);
this.comboBox2.TabIndex = 3;
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(20, 104);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(248, 216);
this.dataGrid1.TabIndex = 4;
//
// button1
//
this.button1.Location = new System.Drawing.Point(15, 328);
this.button1.Name = "button1";
this.button1.TabIndex = 5;
this.button1.Text = "确定";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(199, 328);
this.button2.Name = "button2";
this.button2.TabIndex = 6;
this.button2.Text = "保存";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(107, 328);
this.button3.Name = "button3";
this.button3.TabIndex = 7;
this.button3.Text = "取消";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Grade
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(288, 366);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.comboBox2);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Grade";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "成绩维护";
this.Load += new System.EventHandler(this.Grade_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private void Grade_Load(object sender, System.EventArgs e)
{
this.dataGrid1.CaptionText="班级成绩维护";
myTable=new DataTable();
myTable.Columns.Add("序号",typeof(char));
myTable.Columns.Add("姓名",typeof(char));
myTable.Columns.Add("成绩",typeof(char));
this.dataGrid1.DataSource=myTable;
//设置班级列表框下拉列表
string StrConn="workstation id=localhost;Integrated Security=SSPI;database=stu01";
SqlConnection conn=new SqlConnection(StrConn);
conn.Open();
SqlCommand cmd=conn.CreateCommand();
cmd.CommandText="select distinct 班级名称 from 班级";
SqlDataReader dr=cmd.ExecuteReader();
while(dr.Read())
{
this.comboBox1.Items.Add(dr.GetValue(0).ToString().Trim());
}
this.GetCourseName();
}
//获取课程下拉列表
private void GetCourseName()
{
string StrConn="workstation id=localhost;Integrated Security=SSPI;database=stu01";
SqlConnection conn=new SqlConnection(StrConn);
conn.Open();
SqlCommand cmd=conn.CreateCommand();
cmd.CommandText="select distinct 名称 from 课程表";
SqlDataReader dr=cmd.ExecuteReader();
while(dr.Read())
{
this.comboBox2.Items.Add(dr.GetValue(0).ToString().Trim());
}
}
private void button1_Click(object sender, System.EventArgs e)
{
string courseID=this.GetCourseID();
string classID=this.GetClassID();
this.sendStrSQL="select 成绩表.序号,STU.姓名,成绩表.成绩 from STU inner join 班级 on STU.班级编号=班级.班级编号 inner join 成绩表 on STU.学号=成绩表.学号 and 班级.班级编号=成绩表.班级编号 inner join 课程表 on 成绩表.课程编号=课程表.编号"
+" where(成绩表.班级编号='"+classID.Trim()+"') and (成绩表.课程编号='"+courseID.Trim()+"')";//关于内连接SQL语句应该注意的地方查询条件where语句中所用列名必须是sendTableName表中字段,且查询所涉及的所有表都要进行内连接,以在多表之间建立关系
this.ds=link.SelectDataBase(sendStrSQL,sendTableName);
//为数据集添加数据项浏览控制
cmOrders=(CurrencyManager)BindingContext[ds,"STU"];
this.dataGrid1.DataSource=ds;
this.dataGrid1.DataMember="STU";
}
//课程名称和课程编号的相互转换
private string GetCourseID()
{
string strConn="workstation id=localhost;Integrated Security=SSPI;database=stu01";
SqlConnection conn=new SqlConnection(strConn);
conn.Open();
SqlCommand cmd=conn.CreateCommand();
cmd.CommandText="select 编号 from 课程表 where (名称='"+this.comboBox2.Text.Trim()+"')";
object courseID=cmd.ExecuteScalar();
conn.Close();
return courseID.ToString();
}
//班级名称和班级编号之间的相互转换
private string GetClassID()
{
string strConn="workstation id=localhost;Integrated Security=SSPI;database=stu01";
SqlConnection conn=new SqlConnection(strConn);
conn.Open();
SqlCommand cmd=conn.CreateCommand();
cmd.CommandText="select 班级编号 from 班级 where (班级名称='"+this.comboBox1.Text+"')";
object classID=cmd.ExecuteScalar();
conn.Close();
return classID.ToString();
}
private void button3_Click(object sender, System.EventArgs e)
{
ds.RejectChanges();
}
private void button2_Click(object sender, System.EventArgs e)
{
cmOrders.EndCurrentEdit();
string strConn="workstation id=localhost;Integrated Security=SSPI;database=stu01";
SqlConnection conn=new SqlConnection(strConn);
conn.Open();
SqlCommand cmd=conn.CreateCommand();
for(int i=0;i<ds.Tables[0].Rows.Count;i++)
{
if(ds.Tables[0].Rows[i][2].ToString()=="")
continue;
cmd.CommandText="update 成绩表 set 成绩=cast('"+ds.Tables[0].Rows[i][2].ToString().Trim()
+"' as int) where 序号='"+ds.Tables[0].Rows[i][0].ToString().Trim()+"'";
cmd.ExecuteNonQuery();
}
conn.Close();
MessageBox.Show(this.comboBox1.Text+this.comboBox2.Text+"成绩录入或修改完毕","提示");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -