在上一节当中已经介绍了RavenDb的文档设计模式,这一节我们要具体讲一讲如何使用api去访问RavenDb

.连接RavenDb

var documentStore = new DocumentStore { Url = "http://myravendb.mydomain.com/" };

documentStore.Initialize();

var documentStore = new DocumentStore

{

ConnectionStringName = "MyRavenConStr"

};

在app.config中配置如下:

<connectionStrings>

<add name="Local" connectionString="DataDir = ~\Data"/>

<add name="Server" connectionString="Url = http://localhost:8080"/>

<add name="Secure" connectionString="Url = http://localhost:8080;user=beam;password=up;ResourceManagerId=d5723e19-92ad-4531-adad-8611e6e05c8a"/>

</connectionStrings>

参数:

DataDir - embedded mode的参数, 只能实例化一个EmbeddableDocumentStore,

Url -  server mode的参数

User / Password - server mode的参数

Enlist - whatever RavenDB should enlist in distributed transactions. 不适合Silverlight

ResourceManagerId - 可选的, server mode的参数, the Resource Manager Id that will be used by the Distributed Transaction Coordinator (DTC) service to identify Raven. A custom resource manager id will need to be configured for each Raven server instance when Raven is hosted more than once per machine.不适合Silverlight

Database - server mode的参数,不是用内置的数据库

Url的方式:

Url = http://ravendb.mydomain.com
connect to a remote RavenDB instance at ravendb.mydomain.com, to the default database
Url = http://ravendb.mydomain.com;Database=Northwind
connect to a remote RavenDB instance at ravendb.mydomain.com, to the Northwind database there
Url = http://ravendb.mydomain.com;User=user;Password=secret
connect to a remote RavenDB instance at ravendb.mydomain.com, with the specified credentials
DataDir = ~\App_Data\RavenDB;Enlist=False
use embedded mode with the database located in the App_Data\RavenDB folder, without DTC support.
.Session使用案例
//写入
string companyId;

using (var session = documentStore.OpenSession())

{

    var entity = new Company { Name = "Company" };

    session.Store(entity);

    session.SaveChanges();

    companyId = entity.Id;

}

//读取

using (var session = documentStore.OpenSession())

{

    var entity = session.Load<Company>(companyId);

    Console.WriteLine(entity.Name);

}

//删除,一旦删除无法恢复

using (var session = documentStore.OpenSession())

{

session.Delete(existingBlogPost);

session.SaveChanges();

}

下面两种方式也可以删除 session.Advanced.Defer(new DeleteCommandData { Key = "posts/1234" });

session.Advanced.DocumentStore.DatabaseCommands.Delete("posts/1234", null);

.查询

//PageSize

如果没有设置PageSize,客户端调用是一次128条记录,服务端调用是一次1024条记录,远程调用是一次30条记录,可以配置。

RavenDb为了加快查询数据的速度,它在后台使用的是lucene的索引方式,通过linq来生成HTTP RESTful API。

//查询,用linq的方式查询很方便

var results = from blog in session.Query<BlogPost>()

              where blog.Category == "RavenDB"

              select blog;

var results = session.Query<BlogPost>()

    .Where(x => x.Comments.Length >= )

    .ToList();

//分页查询

var results = session.Query<BlogPost>()

    .Skip() // skip 2 pages worth of posts

    .Take() // Take posts in the page size

    .ToArray(); // execute the query

//分页的时候,我们一次取10条,但是我们也要知道总共有多少条数据,我们需要通过TotalResults来获得

RavenQueryStatistics stats;

var results = session.Query<BlogPost>()

    .Statistics(out stats)

    .Where(x => x.Category == "RavenDB")

    .Take()

    .ToArray();

var totalResults = stats.TotalResults;

//跳过指定的临时的数据集,每次查询都记录下上一次查询记录的跳过的查询记录,该值保存在SkippedResults

RavenQueryStatistics stats;

// get the first page

var results = session.Query<BlogPost>()

    .Statistics(out stats)

    .Skip( * ) // retrieve results for the first page

    .Take() // page size is 10

    .Where(x => x.Category == "RavenDB")

    .Distinct()

    .ToArray();

var totalResults = stats.TotalResults;

var skippedResults = stats.SkippedResults;

// get the second page

results = session.Query<BlogPost>()

    .Statistics(out stats)

    .Skip(( * ) + skippedResults) // retrieve results for the second page, taking into account skipped results

    .Take() // page size is 10

    .Where(x => x.Category == "RavenDB")

    .Distinct()

    .ToArray();

//查询出来的数据不一定是最新的,如果stats.IsStale不为true的话,它就报错的啦

if (stats.IsStale)

{

    // Results are known to be stale

}

//设定获取时间,更新时间截止到某个时刻

RavenQueryStatistics stats;

var results = session.Query<Product>()

    .Statistics(out stats)

    .Where(x => x.Price > )

    .Customize(x => x.WaitForNonStaleResultsAsOf(, , , , , , )))

    .ToArray();

//设置查询返回最后一次更新 documentStore.Conventions.DefaultQueryingConsistency = ConsistencyOptions.QueryYourWrites;

RavenDb学习(二)简单的增删查改的更多相关文章

  1. nodejs连接mysql并进行简单的增删查改

    最近在入门nodejs,正好学习到了如何使用nodejs进行数据库的连接,觉得比较重要,便写一下随笔,简单地记录一下 使用在安装好node之后,我们可以使用npm命令,在项目的根目录,安装nodejs ...

  2. Java连接MySQL数据库及简单的增删查改操作

    主要摘自 https://www.cnblogs.com/town123/p/8336244.html https://www.runoob.com/java/java-mysql-connect.h ...

  3. mybatis实现简单的增删查改

    接触一个新技术,首先去了解它的一些基本概念,这项技术用在什么方面的.这样学习起来,方向性也会更强一些.我对于mybatis的理解是,它是一个封装了JDBC的java框架.所能实现的功能是对数据库进行增 ...

  4. MySQL学习-入门语句以及增删查改

    1. SQL入门语句 SQL,指结构化查询语言,全称是 Structured Query Language,是一种 ANSI(American National Standards Institute ...

  5. EF简单的增删查改

    Add /// <summary> /// /// </summary> public void Add() { TestDBEntities2 testdb = new Te ...

  6. MySQL学习笔记1(增删查改)

    创建表: /* 创建数据库 create database 数据库名; */ CREATE DATABASE mybase; /* 使用数据库 use 数据库名 */ USE mybase; /* 创 ...

  7. asp.net MVC最简单的增删查改!(详)

    折腾了两天搞出来,但原理性的东西还不是很懂,废话不多说上图上代码 然后右键models,新建一个数据模型 注意我添加命名为lianxi 添加后如上 接下来在controllers添加控制器还有在Vie ...

  8. 一般处理程序+htm C#l简单的增删查改

    首先引用两个文件一个dll: 数据库表已创建 首先编写数据读取部分 /// <summary> /// 查询 /// </summary> /// <param name ...

  9. Hibernate 的事物简单的增删查改

    Hibernate 是一个优秀的ORM框架体现在: 1. 面向对象设计的软件内部运行过程可以理解成就是在不断创建各种新对象.建立对象之间的关系,调用对象的方法来改变各个对象的状态和对象消亡的过程,不管 ...

随机推荐

  1. windows 中安装及使用 SSH Key

    转自 简书技术博客:https://www.jianshu.com/p/a3b4f61d4747 联系管理员开通ssh功能: 重新创建环境: 下载工具包到本地机器wsCli 0.4 解压后,把相应的w ...

  2. 常用代码之七:静态htm如何包含header.htm和footer.htm。

    要实现这个有多种解决方案,比如asp, php, 服务器端技术,IFrame等,但本文所记录的仅限于用jQuery和纯htm的解决方案. <head> <title></ ...

  3. C#两个DataTable拷贝问题:该行已经属于另一个表的解决方法

    一.DataTable.Rows.Add(DataRow.ItemArray); 二.DataTable.ImportRow(DataRow) 三.设置DataTable的tablename,然后.R ...

  4. SNF微信公众号客户端演示-微信开发客户端能干什么

    关注测试微信号: 关注后菜单页面如下: 一.扫描二维码进行订单查询演示 1.点击菜单“软件产品”->选择“扫描查询” 2.扫描如下二维码进行订单查询演示. 3.扫描结果如下: 二.微信“输入订单 ...

  5. SQLServer2005 CASE WHEN在项目中实例-查询显示值替换

    1.利用SqlServer中的case when来把数据查询出来的数据替换成其它值显示 2.结果对比: 普通select查询出来的结果如下: 用了case when方法后显示结果如下: 3.具体使用代 ...

  6. windows系统安装ubuntu双系统

    近期由于要学习python开发,经常需要用到linux环境.但是一般的编辑和办公在windows环境下有非常的舒服,所以就想装一个双系统.经过几经周折,终于在我的系统上装成功了,在这分享一些安装过程. ...

  7. Git--团队开发必备神器

    花了两天时间专门搞了一下git.整理一下分享给大家.以下我们開始.. . 转载请注明出处: http://blog.csdn.net/Hello_Chillax/article/details/474 ...

  8. 牛腩学用MUI做手机APP

    斗鱼直播间直播学习撸码,最终目标是用MUI做一个手机APP(暂定android平台,攒钱买IPHONE 7SE!!!),直播内容含整个软件APP的制作过程(含后台接口的制作,放到自己买的阿里云服务器, ...

  9. x86-64整数寄存器

  10. VS Code插件Vue2 代码补全工具

    一.简介 此扩展将Vue 2代码片段和语法突出显示添加到Visual Studio代码中. 这个插件基于最新的Vue官方语法高亮文件添加了语法高亮,并且依据Vue 2的API添加了代码片段. 支持语言 ...