?? 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 調用后添加任何構造函數(shù)代碼
//
//定義Dataset1類的對象實例myset
Dataset1 myset = new Dataset1();
SqlConnection sqlcnn = new SqlConnection( connectstr );
string sqlstr = "select * from Stud";
SqlDataAdapter myadapter = new SqlDataAdapter(sqlstr,sqlcnn);
//將從數(shù)據(jù)庫的Stud表中選出的所有數(shù)據(jù)放置在myset的名為Stud表中
myadapter.Fill(myset,"Stud");
sqlstr = "select * from Yuanxi";
SqlDataAdapter myadapter1 = new SqlDataAdapter(sqlstr,sqlcnn);
//將從數(shù)據(jù)庫的Yuanxi表中選出的所有數(shù)據(jù)放置在myset的名為Yuanxi表中
myadapter1.Fill(myset,"Yuanxi");
//設置程序運行時默認顯示的報表
bao1 bb = new bao1();
//設置將水晶報表的數(shù)據(jù)源
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[] {
"學生信息表",
"學生人數(shù)對比情況"});
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 = "教務管理系統(tǒng)";
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);
//將從數(shù)據(jù)庫的students表中選出的所有數(shù)據(jù)放置在myset的名為students表中
myadapter.Fill(myset,"Stud");
sqlstr = "select * from Yuanxi";
SqlDataAdapter myadapter1 = new SqlDataAdapter(sqlstr,sqlcnn);
//將從數(shù)據(jù)庫的Department表中選出的所有數(shù)據(jù)放置在myset的名為Department表中
myadapter1.Fill(myset,"Yuanxi");
//根據(jù)選擇到的不同字符串對報表的數(shù)據(jù)進行綁定
switch ( Int32.Parse(comboBox1.SelectedIndex.ToString()))
{
//選擇了基本信息表
case 0 :
{
bao1 myrpt = new bao1();
myrpt.SetDataSource(myset);
crystalReportViewer1.ReportSource = myrpt;
break;
}
//選擇了年齡人數(shù)對照表
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);
//執(zhí)行插入操作,并返回受影響的行數(shù)
int j = cmd1.ExecuteNonQuery();
//如果受影響的行數(shù)小于一則說明操作失敗了
if ( j<1 )
{
MessageBox.Show("操作失敗!");
sqlcnn.Close();
}
sqlcnn.Close();
}
catch(Exception er)
{
string temp = er.Message.ToString();
sqlcnn.Close();
return;
}
MessageBox.Show("已經(jīng)成功錄入!");
}
private void crystalReportViewer1_Load(object sender, System.EventArgs e)
{
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -