C#开发的MDI架构+Access数据库应用程序(增删改查)
作者:C/S框架网  发布日期:2011/06/16 23:13:36
C#开发的MDI架构+Access数据库应用程序(增删改查)

C#开发的MDI架构+Access数据库应用程序(英文版)

学习要点

1.C#操作Access数据库,实现增/删/改/查功能

2.实现从Excel导入数据到Access数据库

3.简单的MDI框架实现(学习重点!!!)

4.业务逻辑分层(界面展示层,业务逻辑层,数据访问层)

5.接口应用,创建观察者模式

6.C/S结构的系统框架设计

程序截图:

贴图图片

图1:主窗体

贴图图片

图2:导入XLS文件界面


贴图图片 


图3:解决方案



连接Access数据库

public System.Data.OleDb.OleDbConnection CreateExcelConnection(string excelPath)
{
   string connStr = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended
   
   Properties=Excel 8.0;", excelPath);
   OleDbConnection conn = new OleDbConnection(connStr);
   if (conn.State != ConnectionState.Connecting) conn.Open();
   return conn;
}



实现从Excel导入数据到Access数据库
/*
 * 易学原创作品,转载请注明出处
 * 本文来自易学网
www.vjsdn.com 版权所有
 */

public
class SelectExcel
{
   
   public static DataTable Select()
   {
      OpenFileDialog dlg = new OpenFileDialog();
      dlg.Filter = "Microsoft Excel File(*.xls)|*.xls";
      if (DialogResult.OK == dlg.ShowDialog())
      {
         DataTable dt = DataProvider.Instance.GetExcelTable(dlg.FileName, "Gumball&Jawbreaker");
         return dt;
      }
      return null;
   }
   
   public static void FillData()
   {
      
   }
   
   public static DataTable GetSheet(string excelFile)
   {
      OleDbConnection conn = DataProvider.Instance.CreateExcelConnection(excelFile);
      DataTable dt1 = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
      conn.Close();
      return dt1;
   }
}



简单的MDI框架实现

定义IToolbarRegister接口
/// <summary>
/// 工具栏控件接口
/// </summary>
public interface IToolbarRegister : IDisposable
{
   void RegisterButton(IList buttons); //注册按钮
   void Dispose(); //重置工具栏按钮
   
   IButtonInfo CreateSeperator();//创建分隔按钮,如"|"
   
   /// <summary>
   /// 创建工具栏上的按钮.
   /// </summary>
   /// <param name="name">按钮名称:如btnSave,btnClose</param>
   /// <param name="caption">按钮名称</param>
   /// <param name="image">按钮图片</param>
   /// <param name="size">按钮大小</param>
   /// <param name="clickEvent">按钮的Click事件</param>
   /// <returns></returns>
   IButtonInfo CreateButton(string name, string caption, Bitmap image, Size size, OnButtonClick clickEvent);
}


定义MDI子窗体的接口

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
/*
* 易学原创作品,转载请注明出处
* 本文来自易学网www.vjsdn.com 版权所有
*/

namespace VJSDN_P2009.Interfaces
{
   /// <summary>
   /// MDI子窗体的接口
   /// </summary>
   public interface IMdiChildForm
   {
      //子窗体的工具栏组件
      IToolbarRegister ToolbarRegister { get;set; }
      
      // 注册MDI窗体内的ToolBar.param mdiForm ToolBar管理对象的实例
      void RegisterToolBar(IToolbarRegister toolBarRegister);
      void RegisterObserver(IObserver[] observers); //注册子窗子观察者
      
      IList Buttons { get;} //返回窗体的按钮
      void InitButtons(); //初始化窗体的按钮
      
      bool IsClosing { get;set;} //窗体是否在关闭状态
   }
   
   /// <summary>
   /// 系统按钮
   /// </summary>
   public interface ISystemButtons
   {
      IButtonInfo[] GetSystemButtons();
      
      void DoClose(IButtonInfo button); //关闭窗体
      void DoHelp(IButtonInfo button); //打开帮助
   }
   
   /// <summary>
   /// 支持数据操作的接口
   /// </summary>
   public interface IDataOperatable
   {
      IButtonInfo[] GetDataOperatableButtons();
      void DoViewContent(IButtonInfo button);//查看数据
      void DoAdd(IButtonInfo button);
      void DoEdit(IButtonInfo button);
      void DoCancel(IButtonInfo button);
      void DoSave(IButtonInfo button);
      void DoDelete(IButtonInfo button);
   }
   
   /// <summary>
   /// 支持打印功能的接口
   /// </summary>
   public interface IPrintableForm
   {
      IButtonInfo[] GetPrintableButtons();
      
      void DoPreview(IButtonInfo button);
      void DoPrint(IButtonInfo button);
   }
   
   /// <summary>
   /// 支持锁定/解除锁定功能的接口
   /// </summary>
   public interface ILockableForm
   {
      IButtonInfo[] GetLockableButtons();
      
      void DoLock(IButtonInfo button);
      void DoUnlock(IButtonInfo button);
   }
   
}


定义MDI主窗体接口

/// <summary>
/// MDI主窗体接口
/// </summary>
public interface IMdiForm
{
   IToolbarRegister MdiToolbar { get;set;} //主窗体的工具栏
   IObserver[] MdiObservers { get;} //主窗体动态信息观察者
   
   void RegisterMdiButtons(); //注册MDI主窗体的工具栏的按钮
}


MDI主窗体实现

/*
* 易学原创作品,转载请注明出处
* 本文来自易学网www.vjsdn.com 版权所有
*/

public
partial class frmMDI : Form, IMdiForm
{
   //主窗体上的按钮工具栏。ToolBar
   private IToolbarRegister _MdiToolbar = null;
   
   public frmMDI()
   {
      InitializeComponent();
      
      DataService.SetupDBPath();
   }
   
   private void frmMDI_Load(object sender, EventArgs e)
   {
      this._MdiToolbar = new ToolStripRegister(this);
      this.RegisterMdiButtons();
   }
   
   /// <summary>
   /// MDI主窗体的工具栏
   /// </summary>
   public IToolbarRegister MdiToolbar { get { return _MdiToolbar; } set { _MdiToolbar = value; } }
   
   /// <summary>
   /// 注册MDI主窗体功能的按钮
   /// </summary>
   public void RegisterMdiButtons()
   {
      IList btns = new ArrayList();
      
      btns.Add(this.MdiToolbar.CreateButton("btnHelp", "Help",
      global::VJSDN_P2009.Properties.Resources._24_Help, new Size(57, 28), this.DoHelp));
      
      btns.Add(this.MdiToolbar.CreateButton("btnClose", "Close",
      global::VJSDN_P2009.Properties.Resources._24_Exit, new Size(57, 28), this.DoClose));
      
      btns.Add(this.MdiToolbar.CreateSeperator());
      
      btns.Add(this.MdiToolbar.CreateButton("btnLilyInput", "Lily Input",
      global::VJSDN_P2009.Properties.Resources._24_Edit, new Size(57, 28), this.DoLilyInput));
      
      btns.Add(this.MdiToolbar.CreateButton("btnDavidInput", "David Input",
      global::VJSDN_P2009.Properties.Resources._24_Edit, new Size(57, 28), this.DoDavidInput));
      
      btns.Add(this.MdiToolbar.CreateButton("btnWaltInput", "Walt Input",
      global::VJSDN_P2009.Properties.Resources._24_Edit, new Size(57, 28), this.DoWaltInput));
      
      btns.Add(this.MdiToolbar.CreateSeperator());
      
      btns.Add(this.MdiToolbar.CreateButton("btnProductInquire", "Product Inquire",
      global::VJSDN_P2009.Properties.Resources._24_ViewContent, new Size(57, 28), this.DoInquireProduct));
      
      this.MdiToolbar.RegisterButton(btns);
   }
   
   /// <summary>
   /// MDI主窗体观察者列表
   /// </summary>
   public IObserver[] MdiObservers
   {
      get
      {
         IObserver[] os = new IObserver[2];
         os[0] = new ObserverCloseAllChild(this);
         os[1] = new ObserverFormState(this, this.tsFormState);
         return os;
      }
   }
   
   public void DoInquireProduct(IButtonInfo sender)
   {
      //menuItemLily.PerformClick();
   }
   
   public void DoLilyInput(IButtonInfo sender)
   {
      menuItemLily.PerformClick();
   }
   
   public void DoDavidInput(IButtonInfo sender)
   {
      menuItemDave.PerformClick();
   }
   
   public void DoWaltInput(IButtonInfo sender)
   {
      menuItemWalt.PerformClick();
   }
   
   public void DoHelp(IButtonInfo sender)
   {
      new frmtest().Show();
      //new frmAbout().ShowDialog();
   }
   
   public void DoClose(IButtonInfo sender)
   {
      if (Msg.AskQuestion("Exit application?")) this.Close();
   }
   
   private void menuChild1_Click(object sender, EventArgs e)
   {
      
   }
   
   private void menuAboutMDI_Click(object sender, EventArgs e)
   {
      new frmAbout().ShowDialog();
   }
   
   private void menuURL_Click(object sender, EventArgs e)
   {
      MdiTools.OpenIE("http://www.vjsdn.com");
   }
   
   private void menuItemLily_Click(object sender, EventArgs e)
   {
      MdiTools.OpenChildForm(this, typeof(frmLilyInput));
   }
   
   private void menuItemWalt_Click(object sender, EventArgs e)
   {
      MdiTools.OpenChildForm(this, typeof(frmWaltInput));
   }
   
   private void menuItemDave_Click(object sender, EventArgs e)
   {
      MdiTools.OpenChildForm(this, typeof(frmDavidInput));
   }
}



Source Code for VIP:

点击下载附件 (VIP会员下载) 点击下载附件 (如下载失败,请邮件通知我们寄回给您,或QQ:23404761留言.)
上一篇 下一篇