Form1.cs
上传用户:yiyuerguo
上传日期:2014-09-27
资源大小:3781k
文件大小:12k
源码类别:

C#编程

开发平台:

Others

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8. namespace jiaowu
  9. {
  10. /// <summary>
  11. /// Form1 的摘要说明。
  12. /// </summary>
  13. public class Form1 : System.Windows.Forms.Form
  14. {
  15. private CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer1;
  16. /// <summary>
  17. /// 必需的设计器变量。
  18. /// </summary>
  19. private System.ComponentModel.Container components = null;
  20. private System.Windows.Forms.ComboBox comboBox1;
  21. private string  connectstr = "data source=localhost;Initial Catalog=jiaowu;User ID=sa;Password=sa;";
  22. private System.Windows.Forms.GroupBox groupBox2;
  23. private System.Windows.Forms.TextBox textBox9;
  24. private System.Windows.Forms.TextBox textBox8;
  25. private System.Windows.Forms.TextBox textBox6;
  26. private System.Windows.Forms.TextBox textBox5;
  27. private System.Windows.Forms.Label label10;
  28. private System.Windows.Forms.Label label9;
  29. private System.Windows.Forms.Label label7;
  30. private System.Windows.Forms.Label label6;
  31. private System.Windows.Forms.Button StuInfo;
  32.         private string select;
  33. public Form1()
  34. {
  35. //
  36. // Windows 窗体设计器支持所必需的
  37. //
  38. InitializeComponent();
  39. //
  40. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  41. //
  42. //定义Dataset1类的对象实例myset
  43. Dataset1 myset = new Dataset1();
  44. SqlConnection sqlcnn = new SqlConnection( connectstr );
  45.             
  46. string sqlstr = "select * from Stud";
  47. SqlDataAdapter myadapter = new SqlDataAdapter(sqlstr,sqlcnn);
  48. //将从数据库的Stud表中选出的所有数据放置在myset的名为Stud表中
  49. myadapter.Fill(myset,"Stud");
  50. sqlstr = "select * from Yuanxi";
  51. SqlDataAdapter myadapter1 = new SqlDataAdapter(sqlstr,sqlcnn);
  52. //将从数据库的Yuanxi表中选出的所有数据放置在myset的名为Yuanxi表中
  53. myadapter1.Fill(myset,"Yuanxi");
  54. //设置程序运行时默认显示的报表
  55. bao1 bb = new bao1();
  56. //设置将水晶报表的数据源
  57. bb.SetDataSource(myset);
  58. //设置报表查看器中显示的报表
  59. crystalReportViewer1.ReportSource = bb;
  60. }
  61. /// <summary>
  62. /// 清理所有正在使用的资源。
  63. /// </summary>
  64. protected override void Dispose( bool disposing )
  65. {
  66. if( disposing )
  67. {
  68. if (components != null) 
  69. {
  70. components.Dispose();
  71. }
  72. }
  73. base.Dispose( disposing );
  74. }
  75. #region Windows 窗体设计器生成的代码
  76. /// <summary>
  77. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  78. /// 此方法的内容。
  79. /// </summary>
  80. private void InitializeComponent()
  81. {
  82. this.crystalReportViewer1 = new CrystalDecisions.Windows.Forms.CrystalReportViewer();
  83. this.comboBox1 = new System.Windows.Forms.ComboBox();
  84. this.groupBox2 = new System.Windows.Forms.GroupBox();
  85. this.textBox9 = new System.Windows.Forms.TextBox();
  86. this.textBox8 = new System.Windows.Forms.TextBox();
  87. this.textBox6 = new System.Windows.Forms.TextBox();
  88. this.textBox5 = new System.Windows.Forms.TextBox();
  89. this.label10 = new System.Windows.Forms.Label();
  90. this.label9 = new System.Windows.Forms.Label();
  91. this.label7 = new System.Windows.Forms.Label();
  92. this.label6 = new System.Windows.Forms.Label();
  93. this.StuInfo = new System.Windows.Forms.Button();
  94. this.groupBox2.SuspendLayout();
  95. this.SuspendLayout();
  96. // 
  97. // crystalReportViewer1
  98. // 
  99. this.crystalReportViewer1.ActiveViewIndex = -1;
  100. this.crystalReportViewer1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
  101. | System.Windows.Forms.AnchorStyles.Left) 
  102. | System.Windows.Forms.AnchorStyles.Right)));
  103. this.crystalReportViewer1.Location = new System.Drawing.Point(8, 16);
  104. this.crystalReportViewer1.Name = "crystalReportViewer1";
  105. this.crystalReportViewer1.ReportSource = null;
  106. this.crystalReportViewer1.Size = new System.Drawing.Size(384, 360);
  107. this.crystalReportViewer1.TabIndex = 0;
  108. this.crystalReportViewer1.Load += new System.EventHandler(this.crystalReportViewer1_Load);
  109. // 
  110. // comboBox1
  111. // 
  112. this.comboBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  113. this.comboBox1.Items.AddRange(new object[] {
  114.    "学生信息表",
  115.    "学生人数对比情况"});
  116. this.comboBox1.Location = new System.Drawing.Point(416, 56);
  117. this.comboBox1.Name = "comboBox1";
  118. this.comboBox1.Size = new System.Drawing.Size(176, 20);
  119. this.comboBox1.TabIndex = 5;
  120. this.comboBox1.Text = "请选择";
  121. this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
  122. // 
  123. // groupBox2
  124. // 
  125. this.groupBox2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  126. this.groupBox2.BackColor = System.Drawing.Color.Transparent;
  127. this.groupBox2.Controls.Add(this.textBox9);
  128. this.groupBox2.Controls.Add(this.textBox8);
  129. this.groupBox2.Controls.Add(this.textBox6);
  130. this.groupBox2.Controls.Add(this.textBox5);
  131. this.groupBox2.Controls.Add(this.label10);
  132. this.groupBox2.Controls.Add(this.label9);
  133. this.groupBox2.Controls.Add(this.label7);
  134. this.groupBox2.Controls.Add(this.label6);
  135. this.groupBox2.Controls.Add(this.StuInfo);
  136. this.groupBox2.ForeColor = System.Drawing.Color.Black;
  137. this.groupBox2.Location = new System.Drawing.Point(408, 96);
  138. this.groupBox2.Name = "groupBox2";
  139. this.groupBox2.Size = new System.Drawing.Size(192, 248);
  140. this.groupBox2.TabIndex = 6;
  141. this.groupBox2.TabStop = false;
  142. this.groupBox2.Text = "录入信息";
  143. // 
  144. // textBox9
  145. // 
  146. this.textBox9.Location = new System.Drawing.Point(72, 160);
  147. this.textBox9.Name = "textBox9";
  148. this.textBox9.TabIndex = 15;
  149. this.textBox9.Text = "";
  150. // 
  151. // textBox8
  152. // 
  153. this.textBox8.Location = new System.Drawing.Point(72, 120);
  154. this.textBox8.Name = "textBox8";
  155. this.textBox8.TabIndex = 14;
  156. this.textBox8.Text = "";
  157. // 
  158. // textBox6
  159. // 
  160. this.textBox6.Location = new System.Drawing.Point(72, 80);
  161. this.textBox6.Name = "textBox6";
  162. this.textBox6.TabIndex = 12;
  163. this.textBox6.Text = "";
  164. // 
  165. // textBox5
  166. // 
  167. this.textBox5.Location = new System.Drawing.Point(72, 40);
  168. this.textBox5.Name = "textBox5";
  169. this.textBox5.TabIndex = 11;
  170. this.textBox5.Text = "";
  171. // 
  172. // label10
  173. // 
  174. this.label10.ForeColor = System.Drawing.Color.Black;
  175. this.label10.Location = new System.Drawing.Point(8, 160);
  176. this.label10.Name = "label10";
  177. this.label10.Size = new System.Drawing.Size(56, 23);
  178. this.label10.TabIndex = 10;
  179. this.label10.Text = "院系编号";
  180. this.label10.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
  181. // 
  182. // label9
  183. // 
  184. this.label9.ForeColor = System.Drawing.Color.Black;
  185. this.label9.Location = new System.Drawing.Point(32, 120);
  186. this.label9.Name = "label9";
  187. this.label9.Size = new System.Drawing.Size(32, 23);
  188. this.label9.TabIndex = 9;
  189. this.label9.Text = "年龄";
  190. this.label9.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
  191. // 
  192. // label7
  193. // 
  194. this.label7.ForeColor = System.Drawing.Color.Black;
  195. this.label7.Location = new System.Drawing.Point(8, 80);
  196. this.label7.Name = "label7";
  197. this.label7.Size = new System.Drawing.Size(56, 23);
  198. this.label7.TabIndex = 7;
  199. this.label7.Text = "姓名";
  200. this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
  201. // 
  202. // label6
  203. // 
  204. this.label6.ForeColor = System.Drawing.Color.Black;
  205. this.label6.Location = new System.Drawing.Point(24, 40);
  206. this.label6.Name = "label6";
  207. this.label6.Size = new System.Drawing.Size(40, 23);
  208. this.label6.TabIndex = 6;
  209. this.label6.Text = "学号";
  210. this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
  211. // 
  212. // StuInfo
  213. // 
  214. this.StuInfo.Location = new System.Drawing.Point(72, 208);
  215. this.StuInfo.Name = "StuInfo";
  216. this.StuInfo.Size = new System.Drawing.Size(88, 23);
  217. this.StuInfo.TabIndex = 5;
  218. this.StuInfo.Text = "提交";
  219. this.StuInfo.Click += new System.EventHandler(this.StuInfo_Click);
  220. // 
  221. // Form1
  222. // 
  223. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  224. this.ClientSize = new System.Drawing.Size(608, 389);
  225. this.Controls.Add(this.groupBox2);
  226. this.Controls.Add(this.comboBox1);
  227. this.Controls.Add(this.crystalReportViewer1);
  228. this.Name = "Form1";
  229. this.Text = "教务管理系统";
  230. this.groupBox2.ResumeLayout(false);
  231. this.ResumeLayout(false);
  232. }
  233. #endregion
  234. /// <summary>
  235. /// 应用程序的主入口点。
  236. /// </summary>
  237. [STAThread]
  238. static void Main() 
  239. {
  240. Application.Run(new Form1());
  241. }
  242. private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
  243. {
  244. //获取从comboBox1中选取的字符串
  245. select = comboBox1.SelectedText.ToString();
  246. //定义Dataset1类的对象实例myset
  247. Dataset1 myset = new Dataset1();
  248. SqlConnection sqlcnn = new SqlConnection( connectstr );
  249. string sqlstr = "select * from Stud";
  250. SqlDataAdapter myadapter = new SqlDataAdapter(sqlstr,sqlcnn);
  251. //将从数据库的students表中选出的所有数据放置在myset的名为students表中
  252. myadapter.Fill(myset,"Stud");
  253. sqlstr = "select * from Yuanxi";
  254. SqlDataAdapter myadapter1 = new SqlDataAdapter(sqlstr,sqlcnn);
  255. //将从数据库的Department表中选出的所有数据放置在myset的名为Department表中
  256. myadapter1.Fill(myset,"Yuanxi");
  257. //根据选择到的不同字符串对报表的数据进行绑定
  258. switch ( Int32.Parse(comboBox1.SelectedIndex.ToString()))
  259. {
  260. //选择了基本信息表
  261. case 0 :
  262. {
  263. bao1 myrpt = new bao1();
  264. myrpt.SetDataSource(myset);
  265. crystalReportViewer1.ReportSource = myrpt;
  266. break;
  267. }
  268. //选择了年龄人数对照表
  269. case 1 :
  270. {
  271. bao3 myrpt = new bao3();
  272. myrpt.SetDataSource(myset);
  273. crystalReportViewer1.ReportSource = myrpt;
  274. break;
  275. }
  276. }
  277. }
  278. private void StuInfo_Click(object sender, System.EventArgs e)
  279. {
  280. string s_Number;
  281. string s_Name;
  282. int s_Age;
  283. int Y_Number;
  284. //判断必须输入的信息已输入之后在给变量赋值
  285. if( (textBox5.Text!="") && (textBox6.Text!="") && (textBox8.Text!="") &&(textBox9.Text!=""))
  286. {
  287. s_Number = textBox5.Text;
  288. s_Name = textBox6.Text;
  289. s_Age = Int32.Parse(textBox8.Text);
  290. Y_Number = Int32.Parse(textBox9.Text);
  291. }
  292. else
  293. {
  294. MessageBox.Show("请输入完整的信息!");
  295. return;
  296. }
  297. string check = String.Format("select Y_Number from Yuanxi where Y_Number={0}", Y_Number);
  298. 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);
  299. SqlConnection sqlcnn = new SqlConnection( connectstr );
  300. try
  301. {
  302. sqlcnn.Open();
  303. SqlCommand cmd = new SqlCommand(check, sqlcnn);
  304. //查询Dep_Number表中是否存在当前要录入的学生所在的院系
  305. SqlDataReader MyDataReader = cmd.ExecuteReader();
  306. if ( !MyDataReader.Read() )
  307. {
  308. MessageBox.Show("目前还没有此院系!");
  309. MyDataReader.Close();
  310. sqlcnn.Close();
  311. return;
  312. }
  313. MyDataReader.Close();
  314. SqlCommand cmd1 = new SqlCommand(sql, sqlcnn);
  315. //执行插入操作,并返回受影响的行数
  316. int j = cmd1.ExecuteNonQuery();
  317. //如果受影响的行数小于一则说明操作失败了
  318. if ( j<1 )
  319. {
  320. MessageBox.Show("操作失败!");
  321. sqlcnn.Close();
  322. }
  323. sqlcnn.Close();
  324. }
  325. catch(Exception er)
  326. {
  327. string temp = er.Message.ToString();
  328. sqlcnn.Close();
  329. return;
  330. }
  331. MessageBox.Show("已经成功录入!");
  332. }
  333. private void crystalReportViewer1_Load(object sender, System.EventArgs e)
  334. {
  335. }
  336. }
  337. }