【问题标题】:How to create a database connection from C# to a remote couchbase databse如何创建从 C# 到远程 couchbase 数据库的数据库连接
【发布时间】:2016-05-06 05:28:57
【问题描述】:

我是 couchbase 数据库的新手,我第一次尝试编写代码连接到远程 couchbase 服务器。我用 C# 编写了一个控制台应用程序,其中有一个 app.config 文件和 program.cs 文件。

我的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>

而我的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())
                {

                }
            }
        }
    }
}

当我尝试调试代码时,它会在代码行抛出错误

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

错误信息如下

“Couchbase.Cluster”的类型初始化器抛出异常。

内部异常如下

从配置部分“common/logging”获取 Common.Logging 的配置失败

请帮助我。提前感谢

【问题讨论】:

    标签: c# couchbase


    【解决方案1】:

    很遗憾,我无法重现您在此特定设置中看到的错误。但是,对于您的问题,我确实有一个答案:“如何创建从 C# 到远程 couchbase 数据库的数据库连接”。

    CData ADO.NET Provider 是一个驱动程序,可让您像访问关系数据库一样访问您的 Couchbase 数据。这是通过将 N1QL REST API 封装到基于标准的驱动程序中来完成的,该驱动程序包含熟悉的 .NET 数据库功能。

    从桶中查询很简单,如下代码:

    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"]));
      }
    }
    

    您可以下载 2016 驱动程序 here 的免费测试版。

    【讨论】:

    • 使用您的答案,我能够连接到数据库。唯一的问题是,当我尝试执行 select 命令时,我得到一个错误为“[500] 无法执行指定的命令:配置系统无法初始化”错误抛出的行是 CouchbaseCommand cmd = new CouchbaseCommand("SELECT * FROM KVS", connection); CouchbaseDataReader rdr = cmd.ExecuteReader();ExecuteReader 是行我得到错误
    • 我能够解决这个错误。它来自配置文件。谢谢
    • 我很高兴您能够让事情顺利进行!对于以后遇到此错误的任何人,您能否告诉我们您在配置文件中所做的更改?
    【解决方案2】:

    使用 Couchbase .Net 驱动程序,请确保您已按照说明 here 进行设置。

    您能否尝试以下操作,看看是否会出现其他错误?

            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);
          ....
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-05
      • 2016-08-05
      • 1970-01-01
      • 2011-05-06
      • 2016-07-07
      • 1970-01-01
      相关资源
      最近更新 更多