Skip to content

Concepts

Executing queries

The methods below run SurrealQL statements against the database, call a SurrealQL function, and keep variables on the connection that later queries can use. To read and write records with a method for each operation, see data manipulation.

Method

Description

db.Query()

Runs a set of SurrealQL statements against the database

db.RawQuery()

Runs a set of SurrealQL statements against the database, based on a raw SurrealQL query

db.Run()

Runs a SurrealQL function

db.Set(key, value)

Assigns a value as a parameter for this connection

db.Unset(key)

Removes a parameter for this connection

Runs a set of SurrealQL statements against the database.

Method Syntax
await db.Query(sql)

Arguments

Description

sql

Specifies the SurrealQL statements.

cancellationToken

The cancellationToken enables graceful cancellation of asynchronous operations.

// Execute query with params
const string table = "person";
var result = await db.Query(
    $"CREATE person; SELECT * FROM type::table({table});"
);

// Get the first result from the first query
var created = result.GetValue<Person>(0);

// Get all of the results from the second query
var people = result.GetValue<List<Person>>(1);


Runs a set of SurrealQL statements against the database, based on a raw SurrealQL query.

Method Syntax
await db.RawQuery(sql, params)

Arguments

Description

sql

Specifies the SurrealQL statements.

params

Assigns variables which can be used in the query.

cancellationToken

The cancellationToken enables graceful cancellation of asynchronous operations.

// Assign the variable on the connection
var @params = new Dictionary<string, object> { { "table",
    "person" } };
var result = await db.RawQuery(
    "CREATE person; SELECT * FROM type::table($table);",
    @params
);

// Get the first result from the first query
var created = result.GetValue<Person>(0);

// Get all of the results from the second query
var people = result.GetValue<List<Person>>(1);


Runs a SurrealQL function.

Method Syntax
await db.Run(name, version, args)

Arguments

Description

name

The name of the SurrealQL function.

version

The version of the SurrealQL function.

args

The arguments used by the SurrealQL function.

cancellationToken

The cancellationToken enables graceful cancellation of asynchronous operations.

DateTime now = await db.Run<DateTime>("time::now");

string result = await db.Run<string>("string::repeat", ["test", 3]);


Assigns a value as a parameter for this connection.

Method Syntax
await db.Set(key, val)

Arguments

Description

key

Specifies the name of the variable.

value

Assigns the value to the variable name.

cancellationToken

The cancellationToken enables graceful cancellation of asynchronous operations.

// Assign the variable on the connection
await db.Set("name", new Name { FirstName = "Tobie",
    LastName = "Morgan Hitchcock" });

// Use the variable in a subsequent query
await db.Query($"CREATE person SET name = $name");

// Use the variable in a subsequent query
await db.Query(
    $"SELECT * FROM person WHERE name.first_name = $name.first_name"
);


Removes a parameter for this connection.

Method Syntax
await db.Unset(key)

Arguments

Description

key

Specifies the SurrealQL statements.

cancellationToken

The cancellationToken enables graceful cancellation of asynchronous operations.

await db.Unset("name");

Was this page helpful?

Last updated