Get a database object (C#)
When using a client, most Data API interactions require you to connect to a database through a DataAPIClient object.
Connecting to a database is an essential component of most Data API client scripts. For more information, see Get started with the Data API (C#).
For more information about the Data API client hierarchy, see Python client internals, TypeScript client internals, Go client internals, Java client internals, and C# client internals.
Signature
Use the GetDatabase method, which belongs to the DataAPIClient class.
public Database GetDatabase(string apiEndpoint);
public Database GetDatabase(
string apiEndpoint, string token, string keyspace = null
);
public Database GetDatabase(
string apiEndpoint, GetDatabaseOptions options
);
Parameters
| Name | Type | Summary |
|---|---|---|
|
|
A database API endpoint URL. |
|
|
Optional. An application token.
You can use The username and password used to generate the token must be tied to a role that has sufficient permissions to perform the desired operations. For more information about how to generate a token, see Get endpoint and token. If you specified a token when instantiating the |
|
|
Optional.
Specify the working keyspace.
This keyspace is used for all downstream operations unless you later override it with a method such as If you don’t set this, you must specify the keyspace in a downstream method before performing an operation that requires a keyspace. |
|
Optional.
General API options for this operation, including the keyspace and timeout.
Keyspace is required unless you specified a working keyspace when instantiating the |
Examples
The following examples demonstrate how to connect to a database.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static void Main()
{
var client = new DataAPIClient(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
)
);
}
}
Errors
For a full list of Data API error codes and their corresponding messages, see Data API errors and error codes.
For more information about how the client handles underlying Data API errors, see C# client error handling.
Client reference
For more information, see the client reference.