Get a table (C#)
Gets a reference to a table for use with the Data API clients.
|
Ready to write code? See the examples for this method to get started. If you are new to the Data API, check out the quickstart. |
Result
Returns a Table object that corresponds to the specified table name.
This method returns a Table object even for tables that don’t exist.
By default, the Table object is typed as Table<Row>, where Row is Dictionary<string, object>.
You can enable stronger typing by specifying a type when you get the table.
For more information and examples, see Custom typing for tables.
Parameters
Use the GetTable method, which belongs to the Database class.
Method signature
public Table<Row> GetTable(
string tableName, GetTableOptions options = null
);
public Table<TRow> GetTable<TRow>(
string tableName, GetTableOptions options = null
) where TRow : class;
public Table<TRow> GetTable<TRow>(
GetTableOptions options = null
) where TRow : class, new();
| Name | Type | Summary |
|---|---|---|
|
|
The name of the table. |
|
Optional.
Options for this operation.
For more information and examples for general options such as timeout and keyspace, see Customize API interaction.
For options specific to this method, see Method-specific properties of the |
| Name | Type | Summary |
|---|---|---|
|
|
Optional. This only applies to tables that have a vector column with a vectorize embedding provider integration. Use this option to provide the embedding provider API key directly with headers instead of using an API key in the Astra DB KMS. The API key is sent to the Data API for every operation on the table. It is useful when a vectorize integration is configured but no credentials are stored, or when you want to override the stored credentials. For more information, see Manage embedding provider integrations for vectorize. You can use this authentication method only if all affected columns use the same embedding provider. If you use an AWS embedding provider, you must use the |
|
|
If you use an AWS embedding provider, you must use the |
Examples
The following examples demonstrate how to get a table.
Get a table
using DataStax.AstraDB.DataApi;
namespace Examples;
public class Program
{
static void Main()
{
// Get an existing table
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
var table = database.GetTable("TABLE_NAME");
}
}
Get a table and specify the keyspace
By default, this method uses the working keyspace of your database. If you want to use a different keyspace, you must specify the keyspace.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static void Main()
{
// Get an existing table
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
var table = database.GetTable(
"TABLE_NAME",
new GetTableOptions() { Keyspace = "KEYSPACE_NAME" }
);
}
}
Get a table and specify typing
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Tables;
namespace Examples;
// Define the type for the table
[TableName("TABLE_NAME")]
public class Book
{
[ColumnPrimaryKey(1)]
[ColumnName("title")]
public string Title { get; set; } = null!;
[ColumnPrimaryKeySort(1, SortDirection.Ascending)]
[ColumnName("number_of_pages")]
public int NumberOfPages { get; set; }
[ColumnPrimaryKey(2)]
[ColumnName("rating")]
public float Rating { get; set; }
[ColumnName("genres")]
public string[]? Genres { get; set; }
[ColumnName("metadata")]
public Dictionary<string, string>? Metadata { get; set; }
[ColumnPrimaryKeySort(2, SortDirection.Descending)]
[ColumnName("is_checked_out")]
public bool IsCheckedOut { get; set; }
[ColumnName("due_date")]
public DateOnly? DueDate { get; set; }
}
public class Program
{
static void Main()
{
// Get an existing table
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
var table = database.GetTable<Book>();
}
}
Client reference
For more information, see the client reference.