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.

Result

Returns a Database object that you can use to perform CRUD operations on your database

Parameters

Use the GetDatabase method, which belongs to the DataAPIClient class.

Method signature
public Database GetDatabase(string apiEndpoint);
public Database GetDatabase(
  string apiEndpoint, string token, string keyspace = null
);
public Database GetDatabase(
  string apiEndpoint, GetDatabaseOptions options
);
Name Type Summary

apiEndpoint

string

A database API endpoint URL.

token

str

Optional.

An application token. You can use DataAPIClient.UsernamePasswordTokenProvider to generate a token from a username and password.

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 DataAPIClient, that token will be overridden.

keyspace

string

Optional. Specify the working keyspace. This keyspace is used for all downstream operations unless you later override it with a method such as useKeyspace, getCollection, createCollection, getTable, or createTable.

If you don’t set this, you must specify the keyspace in a downstream method before performing an operation that requires a keyspace.

options

GetDatabaseOptions

Optional. General API options for this operation, including the keyspace and timeout. Keyspace is required unless you specified a working keyspace when instantiating the Database object. 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(
      new CommandOptions() { Destination = DataAPIDestination.HCD }
    );

    var database = client.GetDatabase(
      "API_ENDPOINT",
      DataAPIClient.UsernamePasswordTokenProvider(
        "USERNAME",
        "PASSWORD"
      )
    );
  }
}

Client reference

For more information, see the client reference.

Was this helpful?

Give Feedback

How can we improve the documentation?

© Copyright IBM Corporation 2026 | Privacy policy | Terms of use Manage Privacy Choices

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: Contact IBM