📄 student.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace Students
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Student : System.Windows.Forms.Form
{
private CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer1;
private System.Windows.Forms.ComboBox comboBox1;
private string select;
private System.Windows.Forms.Label label1;
private string connectstr = "data source=WJD;Initial Catalog=students;integrated security=SSPI;";
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Student()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
//定义Dataset1类的对象实例myset
Dataset1 myset = new Dataset1();
SqlConnection sqlcnn = new SqlConnection( connectstr );
string sqlstr = "select * from students";
SqlDataAdapter myadapter = new SqlDataAdapter(sqlstr,sqlcnn);
//将从数据库的students表中选出的所有数据放置在myset的名为students表中
myadapter.Fill(myset,"students");
sqlstr = "select * from Department";
SqlDataAdapter myadapter1 = new SqlDataAdapter(sqlstr,sqlcnn);
//将从数据库的Department表中选出的所有数据放置在myset的名为Department表中
myadapter1.Fill(myset,"Department");
//设置程序运行时默认显示的报表
MyRprt1 myrpt = new MyRprt1();
//设置将水晶报表的数据源
myrpt.SetDataSource(myset);
//设置报表查看器中显示的报表
crystalReportViewer1.ReportSource = myrpt;
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.crystalReportViewer1 = new CrystalDecisions.Windows.Forms.CrystalReportViewer();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// crystalReportViewer1
//
this.crystalReportViewer1.ActiveViewIndex = -1;
this.crystalReportViewer1.BackColor = System.Drawing.Color.White;
this.crystalReportViewer1.Location = new System.Drawing.Point(0, 48);
this.crystalReportViewer1.Name = "crystalReportViewer1";
this.crystalReportViewer1.ReportSource = null;
this.crystalReportViewer1.Size = new System.Drawing.Size(816, 640);
this.crystalReportViewer1.TabIndex = 0;
//
// comboBox1
//
this.comboBox1.Items.AddRange(new object[] {
"学生基本信息表",
"学院基本信息表",
"学生人数对比1",
"学生平均年龄对比",
"学生人数对比2"});
this.comboBox1.Location = new System.Drawing.Point(72, 8);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 20);
this.comboBox1.TabIndex = 4;
this.comboBox1.Text = "请选择";
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// label1
//
this.label1.BackColor = System.Drawing.Color.Transparent;
this.label1.ForeColor = System.Drawing.SystemColors.WindowFrame;
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(64, 23);
this.label1.TabIndex = 5;
this.label1.Text = "切换报表";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// Student
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(816, 694);
this.Controls.Add(this.label1);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.crystalReportViewer1);
this.Name = "Student";
this.Text = "学生信息报表系统";
this.Load += new System.EventHandler(this.Student_Load);
this.ResumeLayout(false);
}
#endregion
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
//获取从comboBox1中选取的字符串
select = comboBox1.SelectedText.ToString();
//定义Dataset1类的对象实例myset
Dataset1 myset = new Dataset1();
SqlConnection sqlcnn = new SqlConnection( connectstr );
string sqlstr = "select * from students";
SqlDataAdapter myadapter = new SqlDataAdapter(sqlstr,sqlcnn);
//将从数据库的students表中选出的所有数据放置在myset的名为students表中
myadapter.Fill(myset,"students");
sqlstr = "select * from Department";
SqlDataAdapter myadapter1 = new SqlDataAdapter(sqlstr,sqlcnn);
//将从数据库的Department表中选出的所有数据放置在myset的名为Department表中
myadapter1.Fill(myset,"Department");
//根据选择到的不同字符串对报表的数据进行绑定
switch ( Int32.Parse(comboBox1.SelectedIndex.ToString()))
{
//选择了学生基本信息表
case 0 :
{
MyRprt1 myrpt = new MyRprt1();
myrpt.SetDataSource(myset);
crystalReportViewer1.ReportSource = myrpt;
break;
}
//选择了学院基本信息表
case 1 :
{
MyRprt2 myrpt = new MyRprt2();
myrpt.SetDataSource(myset);
crystalReportViewer1.ReportSource = myrpt;
break;
}
//选择了学生人数对比1
case 2 :
{
MyRprt3 myrpt = new MyRprt3();
myrpt.SetDataSource(myset);
crystalReportViewer1.ReportSource = myrpt;
break;
}
//选择了学生平均年龄对比
case 3 :
{
MyRprt4 myrpt = new MyRprt4();
myrpt.SetDataSource(myset);
crystalReportViewer1.ReportSource = myrpt;
break;
}
//选择了学生人数对比2
case 4 :
{
MyRprt5 myrpt = new MyRprt5();
myrpt.SetDataSource(myset);
crystalReportViewer1.ReportSource = myrpt;
break;
}
}
}
private void Student_Load(object sender, System.EventArgs e)
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -