Oracle轻型的ORM类Dapper

    public int ServiceCounterId { get; set; }

    public String ServiceName { get; set; }

public class ServiceCounterSnapshot
{
    public int Id { get; set; }

private void
SaveServiceSnapshots(IEnumerable<ServiceCounterSnapshot>
snapshots)
        {
            using (var conn = new
SqlConnection(ConfigurationManager.ConnectionStrings[“SqlDiagnosticsDb”].ConnectionString))
            {
                conn.Open();
                foreach (var snapshot in snapshots)
                {
                    // insert new snapshot to the database
                   conn.Execute(
    @”insert into
service_counter_snapshots(ServiceCounterId,SnapshotMachineName,CreationTimeUtc,ServiceCounterValue)
values (
       
@ServiceCounterId,@SnapshotMachineName,@CreationTimeUtc,@ServiceCounterValue)”,
snapshot);
                }

    public override String ToString()
    {
        return String.Format(@”{0}\{1}\{2}\{3}”, MachineName ?? “.”,
CategoryName, CounterName, InstanceName);
    }
}

Execute方法
正如Query方法是寻觅数据的,Execute方法不会招来数据,它与Query方法分外相似,但它总再次来到总数(受影响的行数),而不是一个对象集合【如:insert
update和delete】.

ServiceCounterSnapshot的概念如下:

关于Dapper的一部分拔取和增添的例子

A Look at
Dapper.NET

给力分享新的ORM =>
Dapper

下面是ServiceCounter的定义

1,在填写嵌套对象的时候,只能执行ToList<>方法,否则回报ExecuteReader
须求已开拓且可用的接连。连接的此时此刻景色为已关门,而单个对象不会报错,揣测是using为止后关闭了两次三番,而嵌套对象在map的时候又推行了ExecuteReader,只能够在using甘休以前重返list集合。
2,嵌套对象的参数是相比较多的,紧若是前八个参数,其余参数没用可以设置为null。尤其要小心的是splitOn,这一个参数无法为空,否则会报对象为引用的失实。【splitOn参数的情趣是读取首个对象的的分割列,从哪个列起开始读取首个目的,如若表里的自增进列都为Id,可以安装这一个参数为”Id”】.

    public float? ServiceCounterValue { get; set; }
}

public class ServiceCounter
{
    public int Id { get; set; }

    public String MachineName { get; set; }
    public String CategoryName { get; set; }
    public String CounterName { get; set; }
    public String InstanceName { get; set; }

  • Dapper很快。Dapper的速度接近与IDataReader。
  • Dapper辅助主流数据库
    Mysql,SqlLite,Mssql2000,Mssql2005,Oracle等一比比皆是的数据库
  • 支撑多表并联的目的。帮衬一对多 多对多的关联,并且没侵入性。
  • 原理通过Emit反射IDataReader的系列队列,来很快的拿到和暴发对象
  • Dapper语法卓殊简约。并且毫不迁就数据库的统筹

Query()方法
Query()是IDbConnection伸张方法并且重载了,从数据库里提取消息,并用来填充我们的作业对象模型。

    public String DisplayName { get; set; }
   
    public String DisplayType { get; set; }

Dapper是一个轻型的ORM类。代码就一个SqlMapper.cs文件,重倘使IDbConnection的恢宏方法,编译后就40K的一个很小的dll。官方站点http://code.google.com/p/dapper-dot-net/
,也足以透过Nuget举行安装

                var counters = new List<Tuple<int,
PerformanceCounter>>();
                using (var conn = new
SqlConnection(ConfigurationManager.ConnectionStrings[“SqlDiagnosticsDb”].ConnectionString))
                {
                    conn.Open();
                    string sql = string.Format(“select
Id,ServiceName,CategoryName,CounterName,InstanceName from
service_counters where MachineName='{0}'”,machineName);
Oracle,                    foreach (var counter in conn.Query<ServiceCounter>(sql))
                    {
                        logger.InfoFormat(@”Creating performance
counter: {0}\{1}\{2}\{3}”, counter.MachineName ?? “.”,
counter.CategoryName,
                                            counter.CounterName,
counter.InstanceName);
                        var perfCounter = new
PerformanceCounter(counter.CategoryName, counter.CounterName,
counter.InstanceName, counter.MachineName ?? “.”);
                        counters.Add(new Tuple<int,
PerformanceCounter>(counter.Id, perfCounter));
                        // first value doesn’t matter so we should call
the counter at least once
                        try { perfCounter.NextValue(); }
                        catch { }
                    }
                }

    public DateTime CreationTimeUtc { get; set; }

    /// <summary>
    /// Machine on which the snapshot was taken.
    /// </summary>
    public String SnapshotMachineName { get; set; }

Oracle 1

            }
        }

Dapper也足以加载填充嵌套对象,考虑那样一种景况,考虑到音讯的种类属性,再次来到连串对象。

相关文章