如何创建从 C# 到远程 couchbase 数据库的数据库连接
How to create a database connection from C# to a remote couchbase databse
我是 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"))
报错信息如下
The type initializer for 'Couchbase.Cluster' threw an exception.
内部异常如下
Failed obtaining configuration for Common.Logging from configuration section 'common/logging
请帮我解决这个问题。提前致谢
很遗憾,我无法重现您在此特定设置中看到的错误。但是,我确实可以回答您的问题:"How to create a database connection from C# to a remote couchbase database".
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。
对于 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);
....
我是 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"))
报错信息如下
The type initializer for 'Couchbase.Cluster' threw an exception.
内部异常如下
Failed obtaining configuration for Common.Logging from configuration section 'common/logging
请帮我解决这个问题。提前致谢
很遗憾,我无法重现您在此特定设置中看到的错误。但是,我确实可以回答您的问题:"How to create a database connection from C# to a remote couchbase database".
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。
对于 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);
....