zoukankan      html  css  js  c++  java
  • PetaPoco 使用总结(二)

     

      接着上一篇,上一篇主要介绍了PetaPoco 基本情况,优缺点和基本的查询功能,所以这篇主要介绍的是PetaPoco 的增,删,改等功能。PetaPoco提供了完整的增,删,改,查功能。是代替SqlHelper辅助类的不二选择。

      插入对象:需要指定的表和它的主键。

    var a=new article();
    a.title
    ="My new article"; a.content="PetaPoco was here"; a.date_created=DateTime.UtcNow;
    db.Insert(
    "articles", "article_id", a);

      如果是T4模板自动生存的Poco 对象,直接  a.Insert() 即可 。

      更新一条数据或是更新某个字段:

    a.content="Balah balah";
    db.Update(a);

      

      删除

    // Delete an article extracting the primary key from a record
    db.Delete("articles", "article_id", a);
    
     
    // Or if you already have the ID elsewhere
    db.Delete("articles", "article_id", null, 123);

      定义Poco类,或者通过T4模板生成,这样增删改查会更加简单:

    // Represents a record in the "articles" table
    [PetaPoco.TableName("articles")]
    [PetaPoco.PrimaryKey("article_id")]
    [PetaPoco.ExplicitColumns]
    public class article
    {
    
        [PetaPoco.Column]publiclong article_id { get; set;}
    
        [PetaPoco.Column]publicstring title { get; set;}
    
        [PetaPoco.Column]publicDateTime date_created { get; set;}
    
        [PetaPoco.Column]public bool draft { get; set;}
    
        [PetaPoco.Column]publicstring content { get; set;}
    
    }

      增加

    var a=new article();
    
    a.title="My new article";
    
    a.content="PetaPoco was here";
    
    a.date_created=DateTime.UtcNow;
    
    db.Insert(a);

      修改

    a.content="Blah blah";
    
    db.Update(a);

      删除对象

    db.Delete(a);

      删除某条或多条记录

    db.Delete<article>("WHERE article_id=@0", 123);

      修改一个对象的单独几个字段:

    db.Update<article>("SET title=@0 WHERE article_id=@1", "New Title", 123);

      同时,你可以告诉PetaPoco 忽略某个字段,给该字段加上 PetaPoco.Ignore 特性 即可

    public class article
    {
        [PetaPoco.Ignore]
        public long SomeCalculatedFieldPerhaps
        {
            get; set;
        }
    }
  • 相关阅读:
    HDU 4278 Faulty Odometer 8进制转10进制
    hdu 4740 The Donkey of Gui Zhou bfs
    hdu 4739 Zhuge Liang's Mines 随机化
    hdu 4738 Caocao's Bridges tarjan
    Codeforces Gym 100187M M. Heaviside Function two pointer
    codeforces Gym 100187L L. Ministry of Truth 水题
    Codeforces Gym 100187K K. Perpetuum Mobile 构造
    codeforces Gym 100187J J. Deck Shuffling dfs
    codeforces Gym 100187H H. Mysterious Photos 水题
    windows服务名称不是单个单词的如何启动?
  • 原文地址:https://www.cnblogs.com/zhangweizhong/p/3718841.html
Copyright © 2011-2022 走看看