简体   繁体   中英

C# to get database and collection in cosmos db sql api

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

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

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