📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace Exam6_3_2
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.DataGrid dataGrid1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// 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.comboBox1 = new System.Windows.Forms.ComboBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// comboBox1
//
this.comboBox1.Location = new System.Drawing.Point(62, 3);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(136, 20);
this.comboBox1.TabIndex = 0;
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(54, 17);
this.label1.TabIndex = 1;
this.label1.Text = "课程名称";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(96, 149);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(42, 17);
this.label2.TabIndex = 3;
this.label2.Text = "人数:";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(176, 149);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(54, 17);
this.label3.TabIndex = 4;
this.label3.Text = "平均分:";
//
// dataGrid1
//
this.dataGrid1.CaptionVisible = false;
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(0, 32);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.RowHeadersVisible = false;
this.dataGrid1.Size = new System.Drawing.Size(320, 112);
this.dataGrid1.TabIndex = 5;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(320, 173);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.comboBox1);
this.Name = "Form1";
this.Text = "成绩查询";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private SqlDataAdapter sqlDataAdapter;
private DataSet dataSet;
private void Form1_Load(object sender, System.EventArgs e)
{
comboBox1.Items.Add("所有");
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString = "server=localhost;database=xsgl;uid=sa;pwd=;";
SqlCommand sqlCommand=new SqlCommand();
sqlCommand.Connection=sqlConnection;;
sqlCommand.CommandType=CommandType.StoredProcedure;
//调用存储过程getCourseName,将课程名称读取到组合框
sqlCommand.CommandText="getCourseName";
sqlConnection.Open();
SqlDataReader dReader=sqlCommand.ExecuteReader();
while(dReader.Read())
{
comboBox1.Items.Add(dReader["courseName"].ToString().Trim());
}
dReader.Close();
sqlConnection.Close();
//调用存储过程stat,先读取所有成绩
sqlCommand.CommandText="stat";
sqlCommand.Parameters.Add("@count",System.Data.SqlDbType.Int);
sqlCommand.Parameters["@count"].Direction=ParameterDirection.ReturnValue;
sqlCommand.Parameters.Add("@courseName",System.Data.SqlDbType.VarChar,30);
sqlCommand.Parameters.Add("@avgScore",System.Data.SqlDbType.Float);
sqlCommand.Parameters["@avgScore"].Direction=ParameterDirection.Output;
sqlDataAdapter=new SqlDataAdapter();
dataSet=new DataSet();
sqlDataAdapter.SelectCommand=sqlCommand;
sqlDataAdapter.SelectCommand.Parameters["@courseName"].Value="%";
sqlDataAdapter.Fill(dataSet,"StudentScore");
dataGrid1.DataSource=dataSet;
dataGrid1.DataMember="StudentScore";
comboBox1.SelectedIndex=0;
}
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
if(comboBox1.SelectedIndex==0)
sqlDataAdapter.SelectCommand.Parameters["@courseName"].Value="%";
else
sqlDataAdapter.SelectCommand.Parameters["@courseName"].Value=comboBox1.SelectedItem;
try
{
dataSet.Tables["StudentScore"].Clear();
sqlDataAdapter.Fill(dataSet,"StudentScore");
label3.Text="平均分:"+sqlDataAdapter.SelectCommand.Parameters["@avgScore"].Value.ToString().Trim();
label2.Text="人数:"+sqlDataAdapter.SelectCommand.Parameters["@count"].Value.ToString().Trim();
}
catch(SqlException ee){System.Console.WriteLine(ee.Message);}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -