简体   繁体   English

如何创建从C#到远程Couchbase数据库的数据库连接

[英]How to create a database connection from C# to a remote couchbase databse

I am kind of new to couchbase database and trying to write a code to connect to a remote couchbase server for my first time. 我对Couchbase数据库很陌生,并且第一次尝试编写代码以连接到远程Couchbase服务器。 I have written a console application in C# where i have a app.config file and the program.cs file. 我已经用C#编写了一个控制台应用程序,其中有一个app.config文件和program.cs文件。

My app.config file is as follows 我的app.config文件如下

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <configSections>
    <sectionGroup name="couchbaseClients">
      <section name="couchbase"
               type="Couchbase.Configuration.Client.Providers.CouchbaseClientSection, Couchbase.NetClient"/>
    </sectionGroup>
  </configSections>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <couchbaseClients>
    <couchbase useSsl="false" operationLifeSpan="1000">
      <servers>
        <add uri="http://xxx.xxx.xxx.xxx:8091/pools"></add>
        <add uri="http://xxx.xxx.xxx.xxx:8091/pools"></add>
      </servers>
      <buckets>
        <add name="default" useSsl="false" Username="xxxxxx" password="xxxxx" operationLifespan="2000">
          <connectionPool name="custom" maxSize="10" minSize="5" sendTimeout="12000"></connectionPool>
        </add>
      </buckets>
    </couchbase>
  </couchbaseClients>
</configuration>

and my Program.cs file is as follows 我的Program.cs文件如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Couchbase;
using Enyim.Caching.Memcached;
using Newtonsoft.Json;
using System.Configuration;

namespace CouchBase_ConnectionTester
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var cluster = new Cluster("couchbaseClients/couchbase"))
            {
                using (var bucket = cluster.OpenBucket())
                {

                }
            }
        }
    }
}

when I try to debug the code, it throws an error at code line 当我尝试调试代码时,它在代码行引发错误

using (var cluster = new Cluster("couchbaseClients/couchbase"))

error message is as follows 错误信息如下

The type initializer for 'Couchbase.Cluster' threw an exception. “ Couchbase.Cluster”的类型初始值设定项引发了异常。

Inner exception is as follows 内部异常如下

Failed obtaining configuration for Common.Logging from configuration section 'common/logging 无法从配置部分“通用/日志记录”中获取Common.Logging的配置

Please help me on this. 请帮我。 Thanks in advnce 提前感谢

Unfortunately, I wasn't able to reproduce the error that you're seeing in this specific setup. 不幸的是,我无法重现您在此特定设置中看到的错误。 However, I do have an answer to your question: "How to create a database connection from C# to a remote couchbase database". 但是,我确实回答了您的问题:“如何创建从C#到远程Couchbase数据库的数据库连接”。

The CData ADO.NET Provider is a driver that allows you access your Couchbase data as if it were a relational database. CData ADO.NET Provider是一个驱动程序,使您可以像访问关系数据库一样访问Couchbase数据。 This is done by wrapping the N1QL REST API into a standards-based driver that incorporates familiar .NET database functionality. 这是通过将N1QL REST API包装到基于标准的驱动程序中来完成的,该驱动程序包含了熟悉的.NET数据库功能。

Querying from a bucket is as simple as the following code: 从存储桶中进行查询就像以下代码一样简单:

string connectionString = "User='myusername';Password='mypassword';Server='http://couchbase40'";
using (CouchbaseConnection connection = new CouchbaseConnection(connectionString)) {
  CouchbaseCommand cmd = new CouchbaseCommand("SELECT * FROM Customer", connection);
  CouchbaseDataReader rdr = cmd.ExecuteReader();

  while (rdr.Read()) {
    Console.WriteLine(String.Format("\t{0} --> \t\t{1}", rdr["Name"],     rdr["TotalDue"]));
  }
}

You can download a free beta build of the 2016 driver here . 您可以在此处下载2016驱动程序的免费beta版本。

With the Couchbase .Net driver, pls make sure you have followed the instructions here to get it set up fine. 使用Couchbase .Net驱动程序,请确保您已按照此处的说明进行了很好的设置。

Could you try the following and see if you get a different error? 您可以尝试以下方法,看看是否遇到其他错误?

        var config = new ClientConfiguration
        {
            Servers = new List<Uri> { 
                new Uri("http://10.0.0.XX:8091/pools")
            },
            UseSsl = false,
            DefaultOperationLifespan = 1000,
            BucketConfigs = new Dictionary<string, BucketConfiguration>
            {
              {"default", new BucketConfiguration
              {
                  BucketName = "default",
                  UseSsl = false,
                  Password = "",
                  DefaultOperationLifespan = 2000,
                  PoolConfiguration = new PoolConfiguration
                  {
                    MaxSize = 10,
                    MinSize = 5,
                    SendTimeout = 12000
                  }
              }
             }
            }
        };

      Cluster cbCluster = new Cluster(config);
      Document<object> cbDoc = new Document<dynamic> { 
                        Id = _key,
                        Content = new
                        {
                            id = "a"
                        }
                    };

      //UPSERT
      var upsert = cbBucket.Upsert(cbDoc);
      ....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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