zoukankan      html  css  js  c++  java
  • PetaPoco join连接问题

    petapoco是一个轻型的ORM,在这里不做详细介绍了。本文主要对如何对petapoco对象使用join连接,查询相关数据库。

    1.首先创建一个简单的数据库,包含两个表。本文使用sql server 2008创建,如下图所示。

    SubTask表中有三个属性分别为Id、TaskId和Status,Task有两个属性分别为Id和Style,都是简单定义的,如下图所示。

               

    2,创建一个控制台程序,加载petapoco.dll,然后分别创建两个petapoco对象,分别为subtask和task。注意join连接有多种形式,这里我选择left join。同时要注意连接顺序,一定要在创建的对象中定义被连接的对象。如我在subtask类中定义了task类。

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace Demo1
     8 {
     9     [PetaPoco.TableName("SubTask")]
    10     [PetaPoco.PrimaryKey("Id")]
    11     public class subtask
    12     {
    13         public int Id { get; set; }
    14         public int TaskId { get; set; }
    15         public int Status { get; set; }
    16 
    17         [PetaPoco.Ignore]
    18         public task t { get; set; }//这个一定要有,不然会报错!
    19     }
    20 }

    task类如下:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace Demo1
     8 {
     9     [PetaPoco.TableName("Task")]
    10     [PetaPoco.PrimaryKey("Id")]
    11     public class task
    12     {
    13         public int Id { get; set; }
    14         public int Style { get; set; }
    15     }
    16 }

    3,在这之前一定要配置app.config。

     1 <?xml version="1.0" encoding="utf-8" ?>
     2 <configuration>
     3     <startup> 
     4         <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
     5     </startup>
     6    <connectionStrings>
     7      <add name="connection" connectionString="server=.;integrated security=SSPI;database=DemoOne"
     8           providerName="System.Data.SqlClient"/>
     9    </connectionStrings>
    10 </configuration>

    下面就是调用petapoco sql builder创建sql语句了,其实sql语句大小写都可以,没有区别,也是刚学习。left join是显示左侧表的所有信息,右侧按照条件ON显示,没有的话显示为空。

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace Demo1
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             var db = new PetaPoco.Database("connection");
    14             var sql = PetaPoco.Sql.Builder
    15                 .Append("Select subt.*,ta.* From SubTask subt")
    16                 .Append("LEFT JOIN Task ta ON subt.TaskId=ta.Id");
    17 
    18             var sub = db.Query<subtask, task>(sql).ToList();
    19 
    20             foreach (var b in sub)
    21             {
    22                 Console.WriteLine("{0}-{1}-{2}", b.Id, b.TaskId, b.Status);
    23             }
    24 
    25             Console.WriteLine("next...");
    26         
    27         }
    28     }
    29 }

    4,在ssms中,调用使用sql语句显示的结果如下:

      use DemoOne
      select subt.*,ta.* from SubTask subt left join Task ta on subt.TaskId=ta.Id
      go

  • 相关阅读:
    20170705总结
    20170703总结
    .NET 框架程序使用 Win32 API
    青春 就此别过
    Aptana Studio 2启动时提示 Workspace Cannot Be Created 解决办法
    App_GlobalResources.afvubzdv.resources.dll”--“拒绝访问。“
    c# 一维数组和二维数组的定义几种方式<转>.
    C#中Split分隔字符串的应用(C#、split、分隔、字符串)<转>
    C#操作字符串方法总结<转>
    C# 时间格式大全
  • 原文地址:https://www.cnblogs.com/haozhangcool/p/cool_hao_zhang.html
Copyright © 2011-2022 走看看