0

I am writing C# application where trying to retrieve database and collection using following connection strings. But not sure what I am missing.

const string cosmos = @"<connection-string>";
const string cosmos_database = "Enctr";
const string cosmos_collection = "account";

SqlConnection client = new SqlConnection(cosmos);
var database = client.GetDatabase(cosmos_database);
var collection = database.GetCollection<BsonDocument>(cosmos_collection);

Its not allowing me to use below 2 lines:

var database = client.GetDatabase(cosmos_database);
var collection = database.GetCollection<BsonDocument>(cosmos_collection);
7
  • SqlConnection is not a cosmos db client, totally different thing Commented Mar 10, 2021 at 1:23
  • Have a try this: CosmosClient client = new CosmosClient(cosmos); var database = client.GetDatabase(cosmos_database); var collection = database.GetCollection<BsonDocument>(cosmos_collection); Commented Mar 10, 2021 at 1:24
  • Thanks Steve. It complains about CosmosClient. I tried to include using Microsoft.Azure.Cosmos; but it doesn't allow Commented Mar 10, 2021 at 1:40
  • I installed Microsoft.Azure.Cosmos package for CosmosClient. However, the line var collection = database.GetCollection<BsonDocument>(cosmos_collection) is not allowed. It says - "Database doesn't contain definition for GetCollection....." Commented Mar 10, 2021 at 1:49
  • What's your version of SDK? Commented Mar 10, 2021 at 1:56

1 Answer 1

2

As @Crowcoder says, SqlConnection is not a cosmos db client, you should use CosmosClient and use GetContainer() to get your container in Cosmos DB. Try the following code:

const string cosmos = @"<connection-string>";
const string cosmos_database = "Enctr";
const string cosmos_collection = "account";

CosmosClient client = new CosmosClient(cosmos);
var database = client.GetDatabase(cosmos_database);
var collection = database.GetContainer(cosmos_collection);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.