zoukankan      html  css  js  c++  java
  • DbEntry.Net(Lephone Framework) Access ORM:安装和简单使用

    项目中用到Access数据库,之前用的普通Ado.Net 三层。遇到表字段叫多时,就比较费力。想要使用ORM,无奈EF不支持Access。虽然可以改写linq to sql为Linq to Access,多多少少会有些兼容性的问题。这有个demo:http://www.cnblogs.com/wangjikun3/archive/2009/06/20/1507175.html

    这里推荐个ORM框架,DbEntry.Net.官网地址:http://dbentry.codeplex.com/

    轻量级,高性能,免费开源。支持C#,Visual Basic,ASP.Net.也提供 DbEntryDataSource for ASP.NET 2.0和Rails风格 MVC框架和 简单的Ioc 框架.支持Access(03,07),SqlServer(2000,2005,2008),Excel,MySql,Sqlite,Oracle,Firebird,PostgreSQL.

    官网文档:http://dbentry.codeplex.com/documentation

    一.安装和简单使用

    1.首先下载DbEntry.Net安装程序。官网下载:http://dbentry.codeplex.com/releases/view/79532 金山网盘:http://www.kuaipan.cn/file/id_226427209806521434.htm?source=1

      安装之后,打开VS,工具→扩展管理器→已安装的扩展→模板,启用DbEntryClassLibrary.

      如果不希望安装模板可以下载bin.zip。官网下载:http://dbentry.codeplex.com/releases/view/79532 金山网盘:http://www.kuaipan.cn/file/id_226427209806521433.htm?source=1

    2.新建项目,点击Visual C#,选择DbEntryClassLibrary,输入项目名确定。会默认创建一个User类。删除我们创建个Student类。

      未安装模板需要引用dll:Lephone.Core.dll Lephone.Data.dll Lephone.Extra.dll Lephone.Web.dll

        public class Student : DbObjectModel<Student>
        {
            public string StuName { get; set; }
            public int StuAge { get; set; }
        }

    3.新建Winform项目,添加对DbEntryClassLibrary项目引用。

      添加app.config:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section name="Lephone.Settings"
          type="Lephone.Core.Setting.NameValueSectionHandler, Lephone.Core" />
      </configSections>
     
      <Lephone.Settings>
        <add key="AutoCreateTable" value="true" />
        <add key="DataBase" value="@Access : @C:	est.mdb" />
      </Lephone.Settings>
      <startup useLegacyV2RuntimeActivationPolicy="true">
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
      </startup>
    </configuration>
    XML Code

      新建窗体。一个DataGridView,两个TextBox,两个Button。DataGridView新建三个Column。

      

      窗体Load事件加载:

            private void Form1_Load(object sender, EventArgs e)
            {
                RefreshData();
            }
    
            /// <summary>
            /// 刷新数据
            /// </summary>
            private void RefreshData()
            {
                dataGridView1.DataSource = Student.Find(Condition.Empty);
            }

      保存按钮:

      /// <summary>
            /// 保存事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnSave_Click(object sender, EventArgs e)
            {
                if (btnSave.Tag != null && btnSave.Tag.ToString() != String.Empty)
                {
                    Student stu = Student.FindById(int.Parse(btnSave.Tag.ToString()));
                    int age = 0; int.TryParse(txtStuAge.Text, out age);
                    stu.StuAge = age;
                    stu.StuName = txtStuName.Text;
                    DbEntry.Save(stu);
                }
                else
                {
                    int age = 0; int.TryParse(txtStuAge.Text, out age);
                    Student stu = new Student() { StuAge = age, StuName = txtStuName.Text };
                    DbEntry.Save(stu);
                }
                RefreshData();
            }

      选择行事件:

      /// <summary>
            /// 选择行事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                if (e.RowIndex >= 0)
                {
                    DataGridViewRow dr = this.dataGridView1.Rows[e.RowIndex];
                    btnSave.Tag = dr.Cells["ID"].Value;
                    txtStuName.Text = dr.Cells["Name"].Value.ToString();
                    txtStuAge.Text = dr.Cells["Age"].Value.ToString();
                }
            }

    删除按钮事件:

        /// <summary>
            /// 删除代码
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnDelete_Click(object sender, EventArgs e)
            {
                if (btnSave.Tag != null && btnSave.Tag.ToString() != String.Empty)
                {
                    Student.DeleteBy(t => t.Id == int.Parse(btnSave.Tag.ToString()));
                }
                RefreshData();
            }

    源代码下载:金山网盘:http://www.kuaipan.cn/file/id_226427209806521436.htm?source=1

  • 相关阅读:
    cesium学习——cesium中的坐标
    webService框架CXF的简单使用
    使用cesium中的scene.open中遇到的几个问题
    通过Spring读取properties配置文件
    常用的Ant风格书写
    oracle小知识点
    虚拟机centos7系统下安装hadoop ha和yarn ha(详细)
    java 注解
    Guava Immutable 不可变集合
    Guava BiMap
  • 原文地址:https://www.cnblogs.com/xcong/p/3602311.html
Copyright © 2011-2022 走看看