Enter dynamic

After we finished Basic CRUD, let's continue enter the world of dynamic object.

  • First, create a new c:\test.mdb file.
  • Open User.cs and change it as following:
using Lephone.Data.Definition;
 
public abstract class User : DbObjectModel<User>
{
    public abstract string Name { get; set; }
    public abstract User Init(string Name);
}
  • Open Program.cs and change it as following:
using System;
using Lephone.Data;
 
class Program
{
    static void Main()
    {
        // Insert
        User.New.Init("jerry").Save();
        User u = User.New.Init("mike");
        u.Save();
        Console.WriteLine("Insert :\n{0}\n", u);
        // Select
        User u1 = User.FindById(u.Id);
        Console.WriteLine("Select :\n{0}\n", u1);
        // Update
        u1.Name = "tom";
        u1.Save();
        Console.WriteLine("Update :\n{0}\n", u1);
        // Select All
        ShowAll("Select all :");
        // Delete
        u1.Delete();
        ShowAll("After delete :");

        Console.ReadLine();
    }
 
    static void ShowAll(string msg)
    {
        Console.WriteLine(msg);
        foreach(User o in User.FindAll(new OrderBy("Id")))
        {
            Console.WriteLine(o);
        }
        Console.WriteLine();
    }
}
  • Run this application, it will shows:
Insert :
{ Id = 2, Name = mike }

Select :
{ Id = 2, Name = mike }

Update :
{ Id = 2, Name = tom }

Select all :
{ Id = 1, Name = jerry }
{ Id = 2, Name = tom }

After delete :
{ Id = 1, Name = jerry }
  • Open the c:\test.mdb file, confirm the User table is following:
Id Name
1 jerry

Last edited Dec 10, 2009 at 6:41 AM by lifeng, version 9

Comments

No comments yet.