📄 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 jiaowu
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.ComboBox comboBox1;
private string connectstr = "data source=localhost;Initial Catalog=jiaowu;User ID=sa;Password=sa;";
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.TextBox textBox9;
private System.Windows.Forms.TextBox textBox8;
private System.Windows.Forms.TextBox textBox6;
private System.Windows.Forms.TextBox textBox5;
private System.Windows.Forms.Label label10;
private System.Windows.Forms.Label label9;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Button StuInfo;
private string select;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
//定义Dataset1类的对象实例myset
Dataset1 myset = new Dataset1();
SqlConnection sqlcnn = new SqlConnection( connectstr );
string sqlstr = "select * from Stud";
SqlDataAdapter myadapter = new SqlDataAdapter(sqlstr,sqlcnn);
//将从数据库的Stud表中选出的所有数据放置在myset的名为Stud表中
myadapter.Fill(myset,"Stud");
sqlstr = "select * from Yuanxi";
SqlDataAdapter myadapter1 = new SqlDataAdapter(sqlstr,sqlcnn);
//将从数据库的Yuanxi表中选出的所有数据放置在myset的名为Yuanxi表中
myadapter1.Fill(myset,"Yuanxi");
//设置程序运行时默认显示的报表
bao1 bb = new bao1();
//设置将水晶报表的数据源
bb.SetDataSource(myset);
//设置报表查看器中显示的报表
crystalReportViewer1.ReportSource = bb;
}
/// <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.crystalReportViewer1 = new CrystalDecisions.Windows.Forms.CrystalReportViewer();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.textBox9 = new System.Windows.Forms.TextBox();
this.textBox8 = new System.Windows.Forms.TextBox();
this.textBox6 = new System.Windows.Forms.TextBox();
this.textBox5 = new System.Windows.Forms.TextBox();
this.label10 = new System.Windows.Forms.Label();
this.label9 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.StuInfo = new System.Windows.Forms.Button();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// crystalReportViewer1
//
this.crystalReportViewer1.ActiveViewIndex = -1;
this.crystalReportViewer1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.crystalReportViewer1.Location = new System.Drawing.Point(8, 16);
this.crystalReportViewer1.Name = "crystalReportViewer1";
this.crystalReportViewer1.ReportSource = null;
this.crystalReportViewer1.Size = new System.Drawing.Size(384, 360);
this.crystalReportViewer1.TabIndex = 0;
this.crystalReportViewer1.Load += new System.EventHandler(this.crystalReportViewer1_Load);
//
// comboBox1
//
this.comboBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.comboBox1.Items.AddRange(new object[] {
"学生信息表",
"学生人数对比情况"});
this.comboBox1.Location = new System.Drawing.Point(416, 56);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(176, 20);
this.comboBox1.TabIndex = 5;
this.comboBox1.Text = "请选择";
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// groupBox2
//
this.groupBox2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.groupBox2.BackColor = System.Drawing.Color.Transparent;
this.groupBox2.Controls.Add(this.textBox9);
this.groupBox2.Controls.Add(this.textBox8);
this.groupBox2.Controls.Add(this.textBox6);
this.groupBox2.Controls.Add(this.textBox5);
this.groupBox2.Controls.Add(this.label10);
this.groupBox2.Controls.Add(this.label9);
this.groupBox2.Controls.Add(this.label7);
this.groupBox2.Controls.Add(this.label6);
this.groupBox2.Controls.Add(this.StuInfo);
this.groupBox2.ForeColor = System.Drawing.Color.Black;
this.groupBox2.Location = new System.Drawing.Point(408, 96);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(192, 248);
this.groupBox2.TabIndex = 6;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "录入信息";
//
// textBox9
//
this.textBox9.Location = new System.Drawing.Point(72, 160);
this.textBox9.Name = "textBox9";
this.textBox9.TabIndex = 15;
this.textBox9.Text = "";
//
// textBox8
//
this.textBox8.Location = new System.Drawing.Point(72, 120);
this.textBox8.Name = "textBox8";
this.textBox8.TabIndex = 14;
this.textBox8.Text = "";
//
// textBox6
//
this.textBox6.Location = new System.Drawing.Point(72, 80);
this.textBox6.Name = "textBox6";
this.textBox6.TabIndex = 12;
this.textBox6.Text = "";
//
// textBox5
//
this.textBox5.Location = new System.Drawing.Point(72, 40);
this.textBox5.Name = "textBox5";
this.textBox5.TabIndex = 11;
this.textBox5.Text = "";
//
// label10
//
this.label10.ForeColor = System.Drawing.Color.Black;
this.label10.Location = new System.Drawing.Point(8, 160);
this.label10.Name = "label10";
this.label10.Size = new System.Drawing.Size(56, 23);
this.label10.TabIndex = 10;
this.label10.Text = "院系编号";
this.label10.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// label9
//
this.label9.ForeColor = System.Drawing.Color.Black;
this.label9.Location = new System.Drawing.Point(32, 120);
this.label9.Name = "label9";
this.label9.Size = new System.Drawing.Size(32, 23);
this.label9.TabIndex = 9;
this.label9.Text = "年龄";
this.label9.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// label7
//
this.label7.ForeColor = System.Drawing.Color.Black;
this.label7.Location = new System.Drawing.Point(8, 80);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(56, 23);
this.label7.TabIndex = 7;
this.label7.Text = "姓名";
this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// label6
//
this.label6.ForeColor = System.Drawing.Color.Black;
this.label6.Location = new System.Drawing.Point(24, 40);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(40, 23);
this.label6.TabIndex = 6;
this.label6.Text = "学号";
this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// StuInfo
//
this.StuInfo.Location = new System.Drawing.Point(72, 208);
this.StuInfo.Name = "StuInfo";
this.StuInfo.Size = new System.Drawing.Size(88, 23);
this.StuInfo.TabIndex = 5;
this.StuInfo.Text = "提交";
this.StuInfo.Click += new System.EventHandler(this.StuInfo_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(608, 389);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.crystalReportViewer1);
this.Name = "Form1";
this.Text = "教务管理系统";
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
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 Stud";
SqlDataAdapter myadapter = new SqlDataAdapter(sqlstr,sqlcnn);
//将从数据库的students表中选出的所有数据放置在myset的名为students表中
myadapter.Fill(myset,"Stud");
sqlstr = "select * from Yuanxi";
SqlDataAdapter myadapter1 = new SqlDataAdapter(sqlstr,sqlcnn);
//将从数据库的Department表中选出的所有数据放置在myset的名为Department表中
myadapter1.Fill(myset,"Yuanxi");
//根据选择到的不同字符串对报表的数据进行绑定
switch ( Int32.Parse(comboBox1.SelectedIndex.ToString()))
{
//选择了基本信息表
case 0 :
{
bao1 myrpt = new bao1();
myrpt.SetDataSource(myset);
crystalReportViewer1.ReportSource = myrpt;
break;
}
//选择了年龄人数对照表
case 1 :
{
bao3 myrpt = new bao3();
myrpt.SetDataSource(myset);
crystalReportViewer1.ReportSource = myrpt;
break;
}
}
}
private void StuInfo_Click(object sender, System.EventArgs e)
{
string s_Number;
string s_Name;
int s_Age;
int Y_Number;
//判断必须输入的信息已输入之后在给变量赋值
if( (textBox5.Text!="") && (textBox6.Text!="") && (textBox8.Text!="") &&(textBox9.Text!=""))
{
s_Number = textBox5.Text;
s_Name = textBox6.Text;
s_Age = Int32.Parse(textBox8.Text);
Y_Number = Int32.Parse(textBox9.Text);
}
else
{
MessageBox.Show("请输入完整的信息!");
return;
}
string check = String.Format("select Y_Number from Yuanxi where Y_Number={0}", Y_Number);
string sql = String.Format("insert into Stud (s_Number,s_Name,s_Age,Y_Number) values ('{0}','{1}',{2},{3})",s_Number,s_Name,s_Age,Y_Number);
SqlConnection sqlcnn = new SqlConnection( connectstr );
try
{
sqlcnn.Open();
SqlCommand cmd = new SqlCommand(check, sqlcnn);
//查询Dep_Number表中是否存在当前要录入的学生所在的院系
SqlDataReader MyDataReader = cmd.ExecuteReader();
if ( !MyDataReader.Read() )
{
MessageBox.Show("目前还没有此院系!");
MyDataReader.Close();
sqlcnn.Close();
return;
}
MyDataReader.Close();
SqlCommand cmd1 = new SqlCommand(sql, sqlcnn);
//执行插入操作,并返回受影响的行数
int j = cmd1.ExecuteNonQuery();
//如果受影响的行数小于一则说明操作失败了
if ( j<1 )
{
MessageBox.Show("操作失败!");
sqlcnn.Close();
}
sqlcnn.Close();
}
catch(Exception er)
{
string temp = er.Message.ToString();
sqlcnn.Close();
return;
}
MessageBox.Show("已经成功录入!");
}
private void crystalReportViewer1_Load(object sender, System.EventArgs e)
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -