📄 mainform.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlServerCe;
using System.IO;
namespace StudInfo
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.MenuItem menuItem6;
private System.Windows.Forms.MenuItem menuItem7;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private SqlCeConnection conn;
private SqlCeCommand cmd;
private SqlCeEngine sqlEngine;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.Button button7;
private System.Windows.Forms.StatusBar statusBar1;
private DataSet ds;
public MainForm()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.menuItem6 = new System.Windows.Forms.MenuItem();
this.menuItem7 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.button7 = new System.Windows.Forms.Button();
this.statusBar1 = new System.Windows.Forms.StatusBar();
//
// dataGrid1
//
this.dataGrid1.Location = new System.Drawing.Point(8, 32);
this.dataGrid1.Size = new System.Drawing.Size(536, 248);
this.dataGrid1.Text = "dataGrid1";
this.dataGrid1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.dataGrid1_MouseUp);
//
// mainMenu1
//
this.mainMenu1.MenuItems.Add(this.menuItem1);
this.mainMenu1.MenuItems.Add(this.menuItem2);
//
// menuItem1
//
this.menuItem1.MenuItems.Add(this.menuItem3);
this.menuItem1.MenuItems.Add(this.menuItem4);
this.menuItem1.MenuItems.Add(this.menuItem5);
this.menuItem1.MenuItems.Add(this.menuItem6);
this.menuItem1.MenuItems.Add(this.menuItem7);
this.menuItem1.Text = "文件";
//
// menuItem3
//
this.menuItem3.Text = "创建数据库";
this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
//
// menuItem4
//
this.menuItem4.Text = "创建学生表";
this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
//
// menuItem5
//
this.menuItem5.Text = "连接数据库";
this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
//
// menuItem6
//
this.menuItem6.Text = "关闭数据库";
this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click);
//
// menuItem7
//
this.menuItem7.Text = "退出";
this.menuItem7.Click += new System.EventHandler(this.menuItem7_Click);
//
// menuItem2
//
this.menuItem2.Text = "帮助";
//
// button1
//
this.button1.Location = new System.Drawing.Point(32, 298);
this.button1.Size = new System.Drawing.Size(88, 24);
this.button1.Text = "添加";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(149, 298);
this.button2.Size = new System.Drawing.Size(88, 24);
this.button2.Text = "编辑";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(274, 298);
this.button3.Size = new System.Drawing.Size(88, 24);
this.button3.Text = "删除";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(391, 298);
this.button4.Size = new System.Drawing.Size(88, 24);
this.button4.Text = "查询";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button5
//
this.button5.Location = new System.Drawing.Point(55, 338);
this.button5.Size = new System.Drawing.Size(112, 32);
this.button5.Text = "显示全部";
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// button6
//
this.button6.Location = new System.Drawing.Point(203, 338);
this.button6.Size = new System.Drawing.Size(112, 32);
this.button6.Text = "年龄排序";
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// button7
//
this.button7.Location = new System.Drawing.Point(351, 338);
this.button7.Size = new System.Drawing.Size(112, 32);
this.button7.Text = "年龄统计";
this.button7.Click += new System.EventHandler(this.button7_Click);
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 406);
this.statusBar1.Size = new System.Drawing.Size(554, 24);
this.statusBar1.Text = "statusBar1";
//
// MainForm
//
this.ClientSize = new System.Drawing.Size(554, 430);
this.Controls.Add(this.statusBar1);
this.Controls.Add(this.button7);
this.Controls.Add(this.button6);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGrid1);
this.MaximizeBox = false;
this.Menu = this.mainMenu1;
this.MinimizeBox = false;
this.Text = "学生信息管理";
this.Load += new System.EventHandler(this.MainForm_Load_1);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
static void Main()
{
Application.Run(new MainForm());
}
private void menuItem5_Click(object sender, System.EventArgs e)
{
SqlCeConnection conn=null;
try
{
conn = new SqlCeConnection ("Data Source=\\My Documents\\studinfo.sdf;password=");
conn.Open();
SqlCeCommand cmd=conn.CreateCommand();
cmd.CommandText="student";
cmd.CommandType=CommandType.TableDirect;
SqlCeDataAdapter da=new SqlCeDataAdapter(cmd);
DataSet ds=new DataSet ();
da.Fill(ds);
DataGridTableStyle TSAuthors = new DataGridTableStyle();
TSAuthors.MappingName = ds.Tables[0].ToString();
// Add a GridColumnStyle and set the MappingName
// to the name of a DataColumn in the DataTable.
// Set the HeaderText and Width properties.
DataGridColumnStyle TCFirstName = new DataGridTextBoxColumn();
TCFirstName.MappingName = "sid";
TCFirstName.HeaderText = "学生编号";
TCFirstName.Width = 75;
TSAuthors.GridColumnStyles.Add(TCFirstName);
//
DataGridColumnStyle TName = new DataGridTextBoxColumn();
TName.MappingName="sname";
TName.HeaderText="姓名";
TName.Width = 75;
TSAuthors.GridColumnStyles.Add(TName);
DataGridColumnStyle Tsex = new DataGridTextBoxColumn();
Tsex.MappingName="ssex";
Tsex.HeaderText="性别";
Tsex.Width = 75;
TSAuthors.GridColumnStyles.Add(Tsex);
DataGridColumnStyle Tage = new DataGridTextBoxColumn();
Tage.MappingName="sage";
Tage.HeaderText="年龄";
Tage.Width = 75;
TSAuthors.GridColumnStyles.Add(Tage);
DataGridColumnStyle Tdept = new DataGridTextBoxColumn();
Tdept.MappingName="sdept";
Tdept.HeaderText="系部";
Tdept.Width = 75;
TSAuthors.GridColumnStyles.Add(Tdept);
// Add the DataGridTableStyle instance to
// the GridTableStylesCollection.
dataGrid1.TableStyles.Add(TSAuthors);
dataGrid1.DataSource=ds.Tables[0];
}
catch
{
MessageBox.Show("连接数据库失败!","连接错误",MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
finally
{
conn.Close();
}
}
private void menuItem3_Click(object sender, System.EventArgs e)
{
sqlEngine=null;
try
{
if (File.Exists("\\My Documents\\studinfo.sdf"))
{
File.Exists("\\My Documents\\studinfo.sdf");
}
sqlEngine=new SqlCeEngine();
sqlEngine.LocalConnectionString="Data Source=\\My Documents\\studinfo.sdf;password=";
sqlEngine.CreateDatabase();
}
catch
{
MessageBox.Show("创建数据库失败!","建库错误",MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
finally
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -