First application

The following steps shows how to create a basic database application using DbEntry.Net:

  • Create a console application in Visual Studio 2010/2012/2013.
  • Create a DbEntryClassLibrary 4.2 project and make the console application references it.
  • Create a new Access mdb file named test.mdb, and store it to c:\Test (Make sure it exists). If you are using Access 2007, please save it as Access 2003 format.
  • Add the references of Leafing.Data.dll and Leafing.Core.dll to this application.
  • Add an App.config to this application. Open it in Visual Studio, and change it as following:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="Leafing.Settings"
      type="Leafing.Core.Setting.NameValueSectionHandler, Leafing.Core" />
  </configSections>
 
  <Leafing.Settings>
    <add key="AutoCreateTable" value="true" />
    <add key="DataBase" value="@Access : @C:\Test\test.mdb" />
  </Leafing.Settings>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>
  • Notice that there is a class file named User.cs in the folder Models in the class library. And the code will be:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Leafing.Data.Definition;

namespace DbEntryClassLibrary1.Models
{
    public class User : DbObjectModel<User>
    {
        public string Name { get; set; }
    }
}
  • Open Program.cs and change it as following:
using System;
using Leafing.Data;
using DbEntryClassLibrary1.Models;
 
class Program
{
    static void Main(string[] args)
    {
        var u = new User {Name = "tom"};
        Console.WriteLine("New object:\n{0}", u);
        u.Save();
        Console.WriteLine("Saved object:\n{0}", u);
        Console.ReadLine();
    }
}
  • Run this application, it will shows:
New object:
{ Id = 0, Name = tom }
Saved object:
{ Id = 1, Name = tom }
  • Check c:\Test\test.mdb file, confirm the User table already created and it has one row data, check the data of this row, confirm the Name column is tom:
Id Name
1 tom
  • Re-run this application, it will shows:
New object:
{ Id = 0, Name = tom }
Saved object:
{ Id = 2, Name = tom }
  • Check the c:\test.mdb file, confirm the data of User table is following:
Id Name
1 tom
2 tom

Last edited Oct 4, 2014 at 3:35 AM by lifeng, version 28

Comments

lifeng Dec 31, 2013 at 3:50 AM 
严格按照顺序,前一篇 Setup 不可跳过!

lovebht Dec 29, 2013 at 3:50 AM 
你好,
我按照这个例子折腾了昨天一晚上也没有出来,而且试了各种方式,源码都看了一遍就是不行。能帮忙看看么,打算学下您这个
1)配置如下
<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="@SqlServer2008 : Data Source=(localdb)\Projects;Initial Catalog=testentry;Integrated Security=True"/>

</Lephone.Settings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
</configuration>
2)
实体对象
public class Person : DbObjectModel<Person>
{
private String _name;
public String Name
{
get { return _name; }
set { _name = value; }
}

private int age;

public int Age
{
get { return age; }
set { age = value; }
}

3)调用方法
try
{

var p = new Person { Age = 15, Name = "dd", Id = 10 };
p.Save();
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
Console.Read();
}

另外我把你的例子下下来编译都过了,但是一运行出一样的错误。
本地都是出 throw new ModelException(Info.HandleType, "Can not find ObjectHandler.");异常。
老师帮忙看看吧,愁死了。