📄 frmtestorm.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace MyORM
{
/// <summary>
/// frmTestORM 的摘要说明。
/// </summary>
public class frmTestORM : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox lstName;
private System.Windows.Forms.Button cmdRefresh;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtName;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtTitleOfCourtesy;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox txtAddress;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox txtNotes;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.TextBox txtID;
private System.Windows.Forms.Button cmdInsert;
private System.Windows.Forms.Button cmdDelete;
private System.Windows.Forms.Button cmdEdit;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public frmTestORM()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <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.lstName = new System.Windows.Forms.ListBox();
this.cmdRefresh = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.txtName = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.txtTitleOfCourtesy = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.txtAddress = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.txtNotes = new System.Windows.Forms.TextBox();
this.label5 = new System.Windows.Forms.Label();
this.txtID = new System.Windows.Forms.TextBox();
this.cmdInsert = new System.Windows.Forms.Button();
this.cmdDelete = new System.Windows.Forms.Button();
this.cmdEdit = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// lstName
//
this.lstName.DisplayMember = "FullName";
this.lstName.ItemHeight = 12;
this.lstName.Location = new System.Drawing.Point(8, 40);
this.lstName.Name = "lstName";
this.lstName.Size = new System.Drawing.Size(192, 244);
this.lstName.TabIndex = 0;
this.lstName.SelectedIndexChanged += new System.EventHandler(this.lstName_SelectedIndexChanged);
//
// cmdRefresh
//
this.cmdRefresh.Location = new System.Drawing.Point(16, 8);
this.cmdRefresh.Name = "cmdRefresh";
this.cmdRefresh.TabIndex = 1;
this.cmdRefresh.Text = "刷新";
this.cmdRefresh.Click += new System.EventHandler(this.cmdRefresh_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(224, 80);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(35, 17);
this.label1.TabIndex = 2;
this.label1.Text = "姓名:";
//
// txtName
//
this.txtName.Location = new System.Drawing.Point(272, 80);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(224, 21);
this.txtName.TabIndex = 3;
this.txtName.Text = "";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(224, 120);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(35, 17);
this.label2.TabIndex = 2;
this.label2.Text = "称呼:";
//
// txtTitleOfCourtesy
//
this.txtTitleOfCourtesy.Location = new System.Drawing.Point(272, 120);
this.txtTitleOfCourtesy.Name = "txtTitleOfCourtesy";
this.txtTitleOfCourtesy.Size = new System.Drawing.Size(224, 21);
this.txtTitleOfCourtesy.TabIndex = 3;
this.txtTitleOfCourtesy.Text = "";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(224, 168);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(35, 17);
this.label3.TabIndex = 2;
this.label3.Text = "地址:";
//
// txtAddress
//
this.txtAddress.Location = new System.Drawing.Point(272, 168);
this.txtAddress.Name = "txtAddress";
this.txtAddress.Size = new System.Drawing.Size(224, 21);
this.txtAddress.TabIndex = 3;
this.txtAddress.Text = "";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(224, 200);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(35, 17);
this.label4.TabIndex = 2;
this.label4.Text = "说明:";
//
// txtNotes
//
this.txtNotes.Location = new System.Drawing.Point(272, 200);
this.txtNotes.Multiline = true;
this.txtNotes.Name = "txtNotes";
this.txtNotes.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.txtNotes.Size = new System.Drawing.Size(224, 88);
this.txtNotes.TabIndex = 3;
this.txtNotes.Text = "";
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(224, 48);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(35, 17);
this.label5.TabIndex = 2;
this.label5.Text = "编号:";
//
// txtID
//
this.txtID.Location = new System.Drawing.Point(272, 48);
this.txtID.Name = "txtID";
this.txtID.Size = new System.Drawing.Size(224, 21);
this.txtID.TabIndex = 3;
this.txtID.Text = "";
//
// cmdInsert
//
this.cmdInsert.Location = new System.Drawing.Point(112, 8);
this.cmdInsert.Name = "cmdInsert";
this.cmdInsert.TabIndex = 1;
this.cmdInsert.Text = "新增";
this.cmdInsert.Click += new System.EventHandler(this.cmdInsert_Click);
//
// cmdDelete
//
this.cmdDelete.Location = new System.Drawing.Point(296, 8);
this.cmdDelete.Name = "cmdDelete";
this.cmdDelete.TabIndex = 1;
this.cmdDelete.Text = "删除";
this.cmdDelete.Click += new System.EventHandler(this.cmdDelete_Click);
//
// cmdEdit
//
this.cmdEdit.Location = new System.Drawing.Point(208, 8);
this.cmdEdit.Name = "cmdEdit";
this.cmdEdit.TabIndex = 1;
this.cmdEdit.Text = "修改";
this.cmdEdit.Click += new System.EventHandler(this.cmdEdit_Click);
//
// frmTestORM
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(520, 318);
this.Controls.Add(this.txtName);
this.Controls.Add(this.label1);
this.Controls.Add(this.label2);
this.Controls.Add(this.txtTitleOfCourtesy);
this.Controls.Add(this.label3);
this.Controls.Add(this.txtAddress);
this.Controls.Add(this.label4);
this.Controls.Add(this.txtNotes);
this.Controls.Add(this.label5);
this.Controls.Add(this.txtID);
this.Controls.Add(this.cmdRefresh);
this.Controls.Add(this.lstName);
this.Controls.Add(this.cmdInsert);
this.Controls.Add(this.cmdDelete);
this.Controls.Add(this.cmdEdit);
this.Name = "frmTestORM";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "测试ORM";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 连接数据库,创建ORM框架对象
/// </summary>
/// <returns>ORM框架对象</returns>
private MyORMFramework CreateFramework()
{
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection();
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.IO.Path.Combine( System.Windows.Forms.Application.StartupPath , "demomdb.mdb" );
conn.Open();
return new MyORMFramework( conn );
}
private void cmdRefresh_Click(object sender, System.EventArgs e)
{
using( MyORMFramework myWork = this.CreateFramework())
{
RefreshList( myWork );
}
}
private void lstName_SelectedIndexChanged(object sender, System.EventArgs e)
{
DB_Employees obj = lstName.SelectedItem as DB_Employees ;
if( obj != null )
{
this.txtID.Text = obj.EmployeeID.ToString() ;
this.txtName.Text = obj.FullName ;
this.txtTitleOfCourtesy.Text = obj.TitleOfCourtesy ;
this.txtAddress.Text = obj.Address ;
this.txtNotes.Text = obj.Notes ;
}
else
{
this.txtID.Text = "";
this.txtName.Text = "";
this.txtTitleOfCourtesy.Text = "";
this.txtNotes.Text = "" ;
this.txtAddress.Text = "";
}
}
private void cmdInsert_Click(object sender, System.EventArgs e)
{
try
{
using( dlgRecord dlg = new dlgRecord())
{
dlg.Employe = new DB_Employees();
if( dlg.ShowDialog( this ) == DialogResult.OK )
{
using( MyORMFramework myWork = this.CreateFramework())
{
if( myWork.InsertObject( dlg.Employe ) > 0 )
{
RefreshList( myWork );
}
}
}
}
}
catch( Exception ext )
{
MessageBox.Show( ext.ToString());
}
}
private void cmdDelete_Click(object sender, System.EventArgs e)
{
DB_Employees obj = this.lstName.SelectedItem as DB_Employees ;
if( obj != null )
{
if( MessageBox.Show(
this ,
"是否删除 " + obj.FullName + " 的纪录?",
"系统提示" ,
System.Windows.Forms.MessageBoxButtons.YesNo ) == DialogResult.Yes )
{
using( MyORMFramework myWork = this.CreateFramework())
{
myWork.DeleteObject( obj );
RefreshList( myWork );
}
}
}
}
private void RefreshList( MyORMFramework myWork )
{
object[] objs = myWork.ReadAllObjects(typeof( DB_Employees ));
System.Collections.ArrayList list = new ArrayList();
list.AddRange( objs );
this.lstName.DataSource = list ;
this.lstName.DisplayMember = "FullName";
}
private void cmdEdit_Click(object sender, System.EventArgs e)
{
DB_Employees obj = this.lstName.SelectedItem as DB_Employees ;
if( obj == null )
return ;
using( dlgRecord dlg = new dlgRecord())
{
dlg.txtID.ReadOnly = true ;
dlg.Employe = obj ;
if( dlg.ShowDialog( this ) == DialogResult.OK )
{
using( MyORMFramework myWork = this.CreateFramework())
{
if( myWork.UpdateObject( obj ) > 0 )
{
RefreshList( myWork );
}
}
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -