📄 gradesearch.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>
/// GradeSearch 的摘要说明。
/// </summary>
public class GradeSearch : System.Windows.Forms.Form
{
private DataSet ds=new DataSet();
private string sendStrSQL;
private string sendTableName="成绩表";
private DataTable myTable;
private LinkDataBase link=new LinkDataBase();
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.ComboBox cobClass;
private System.Windows.Forms.ComboBox cobCourse;
private System.Windows.Forms.Button btnSearch;
private System.Windows.Forms.DataGrid dataGrid1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public GradeSearch()
{
//
// 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.cobClass = new System.Windows.Forms.ComboBox();
this.cobCourse = new System.Windows.Forms.ComboBox();
this.btnSearch = new System.Windows.Forms.Button();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(35, 24);
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(318, 24);
this.label2.Name = "label2";
this.label2.TabIndex = 1;
this.label2.Text = "选择课程";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// cobClass
//
this.cobClass.Location = new System.Drawing.Point(166, 27);
this.cobClass.Name = "cobClass";
this.cobClass.Size = new System.Drawing.Size(121, 20);
this.cobClass.TabIndex = 2;
//
// cobCourse
//
this.cobCourse.Location = new System.Drawing.Point(449, 27);
this.cobCourse.Name = "cobCourse";
this.cobCourse.Size = new System.Drawing.Size(121, 20);
this.cobCourse.TabIndex = 3;
//
// btnSearch
//
this.btnSearch.Location = new System.Drawing.Point(601, 24);
this.btnSearch.Name = "btnSearch";
this.btnSearch.TabIndex = 4;
this.btnSearch.Text = "查询";
this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(20, 72);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.ReadOnly = true;
this.dataGrid1.Size = new System.Drawing.Size(672, 376);
this.dataGrid1.TabIndex = 0;
//
// GradeSearch
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(712, 466);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.btnSearch);
this.Controls.Add(this.cobCourse);
this.Controls.Add(this.cobClass);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "GradeSearch";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "班级成绩查询";
this.Load += new System.EventHandler(this.GradeSearch_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private void GradeSearch_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 班级名称 from 班级";
SqlDataReader dr=cmd.ExecuteReader();
while(dr.Read())
{
this.cobClass.Items.Add(dr.GetValue(0).ToString().Trim());
}
this.GetCourse();
}
/*private void DataGridStateControl()
{
DataGridTableStyle ts=new DataGridTableStyle();
ts.AlternatingBackColor=Color.Gray;
ts.MappingName=ds.Tables[0].TableName;
this.dataGrid1.TableStyles.Add(ts);
}*/
private void GetCourse()
{
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 课程表";
SqlDataReader dr=cmd.ExecuteReader();
while(dr.Read())
{
this.cobCourse.Items.Add(dr.GetValue(0).ToString().Trim());
}
}
private void btnSearch_Click(object sender, System.EventArgs e)
{
if(this.cobCourse.Text==""||this.cobClass.Text=="")
{
MessageBox.Show("查询条件不完整","信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return;
}
string classID=this.GetClassID();
string courseID=this.GetCourseID();
this.sendStrSQL="select STU.学号, STU.姓名, 成绩表.成绩 FROM STU INNER JOIN 班级 ON STU.班级编号 = 班级.班级编号 INNER JOIN 成绩表 ON STU.学号 = 成绩表.学号 AND 班级.班级编号 = 成绩表.班级编号 INNER JOIN 课程表 ON 成绩表.课程编号 = 课程表.编号"
+" where (成绩表.班级编号='"+classID.Trim()+"') AND (成绩表.课程编号='"+courseID.Trim()+"')";
this.ds=link.SelectDataBase(sendStrSQL,sendTableName);
this.dataGrid1.DataSource=ds;
this.dataGrid1.DataMember="成绩表";
//this.DataGridStateControl();
}
//班级名称和班级编号的转换
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.cobClass.Text.Trim()+"')";
object classID=cmd.ExecuteScalar();
return classID.ToString();
}
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.cobCourse.Text.Trim()+"')";
object courseID=cmd.ExecuteScalar();
return courseID.ToString();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -