Get a table (C#)
|
Tables with the Data API are currently in public preview. Development is ongoing, and the features and functionality are subject to change. Hyper-Converged Database (HCD), and the use of such, is subject to the DataStax Preview Terms. |
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.
Signature
Use the GetTable method, which belongs to the Database class.
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();
Parameters
| Name | Type | Summary |
|---|---|---|
|
|
The name of the table. |
|
Optional.
Options for this operation.
Keyspace is required unless you specified a working keyspace when instantiating the |
Examples
The following examples demonstrate how to get a table.
Get a table
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(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
var table = database.GetTable("TABLE_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(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
var table = database.GetTable<Book>();
}
}
Client reference
For more information, see the client reference.