简体   繁体   中英

Connect CouchDB with asp.net C# application

How to connect couchDB with ASP.NET C# application? If any one can you give a sample application.

I had the same need and after evaluating the options available, to meet the requirements of my application, I created any components that helped me a lot and maybe they can help you and also others. I make it clear that I have no intention of promoting myself here, just sharing something that may be useful.

The detailed explanation of how to configure and use it is on Github .

Link: Nuget Package | Github

Example of use for retrieving documents with mango-querie:

IList<User> users;
var sts = new List<String> { "ACTIVE", "LOCKED" };
using (UserRepository db = new UserRepository())
{
    var query = db.FindOf("list-status", new { id = "OwnerIdloop.user.7", statuses = sts });
    users = db.List<User>(query);
}
Array.ForEach(users.ToArray(), Console.WriteLine);

Example of adding documents:

User user = createUser("email@email.com");
using (UserRepository db = new UserRepository())
{
    var result = db.Insert<User>(user); // add document and return instance changed with operation revision id
    Console.WriteLine(result.Revision);
}

Example of changing documents:

using (UserRepository db = new UserRepository())
{
    // Load document data by ID
    var user = db.Get<User>("email@email.com");
    user.Name = user.Name + "::CHANGED";

    var result = db.Update<User>(user); // update document and return instance changed with operation revision id
    Console.WriteLine(result.Revision);
}

Example of deleting documents:

using (UserRepository db = new UserRepository())
{
    // Load document data by ID
    var user = db.Get<User>("email@email.com");

    var result = db.Delete<User>(user); // delete document from database. Return true case sucess or false case not deleted
    Console.WriteLine($"Sucesso: {result}");
}

After installing the NuGet, just create an instance of MyCouch.Client and pass it the URL of your database.

using (var client = new MyCouchClient("http://127.0.0.1:5984/test"))
{
    //Consume here
}

The format is: {scheme}://[{username}:{password}]/{authority}/{localpath} . From v0.11.0, there's a specific MyCouchUriBuilder that you can use for building the Uri. It will automatically eg apply Uri.EscapeDataString to username and password when calling SetBasicCredentials .

var uriBuilder = new MyCouchUriBuilder("http://localhost:5984/")
    .SetDbName(TestConstants.TestDbName)
    .SetBasicCredentials("foob@r", "p@ssword");

return new MyCouchClient(uriBuilder.Build());

For more details Click Here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM