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, such as |
|
|
Optional.
An application token for a database, in the form of an The token should have sufficient permissions to perform the desired operations. 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 Default: default_keyspace |
|
Optional. General API options for this operation, including the keyspace and timeout. For more information and examples, see Customize API interaction. |
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();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
}
}
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.