# Rezoom.SQL.Synchronous

[Home](/rezoom-sql/master.md) > [API](/rezoom-sql/api.md) > Rezoom.SQL.Synchronous

[← Rezoom.SQL](/rezoom-sql/api/rezoomsql.md) | [Rezoom.SQL.Asynchronous →](/rezoom-sql/api/rezoomsqlasynchronous.md)

## Rezoom.SQL.Synchronous

This namespace contains extension methods to run SQL commands synchronously.

### The `ConnectionContext` type

You can use these methods on a `DbConnection` obtained however you like. However, it is more idiomatic to use the `ConnectionContext` type in Rezoom.SQL.

This will open a connection when needed using the `ConnectionProvider` you pass to its constructor. That's typically resolved from your host's `IServiceProvider` via `ConnectionProvider.ResolveFrom(services)`; see [Runtime configuration](/rezoom-sql/configuration/configuration.md). If you have multiple connection names, you can work with all of them using the same `ConnectionContext`, and all the database connections will be disposed when the context is disposed.

Example usage:

```fsharp
open Rezoom.SQL
open Rezoom.SQL.Synchronous

type QueryType = SQL<"select Name, Email from Users where Id = @id">

let example() =
    use context = new ConnectionContext(connectionProvider)
    let results = QueryType.Command(id = 1).Execute(context)
    for result in results do
        printfn "%s %s" result.Name result.Email
```

### Simple execution

These extensions run the given command and return its result directly.

```fsharp
static member Execute : cmd : Command<'a> * conn : DbConnection -> 'a
static member Execute : cmd:Command<'a> * context:ConnectionContext -> 'a
```

### Taking the only result

If you execute a query with a `limit 1` clause, you will probably not want to deal with the list of rows you get back. You'll only care about the first returned row, and you might want to assert that there aren't more rows just as a sanity check. These methods do that. All will throw if the result list contains more than one object. The `TryExactlyOne` variants will accept empty lists and return `None`, while the `ExactlyOne` variants will throw on empty lists.

```fsharp
static member ExecuteTryExactlyOne : cmd : Command<#IReadOnlyList<'a>> * conn : DbConnection -> 'a option
static member ExecuteTryExactlyOne : cmd : Command<#IReadOnlyList<'a>> * context : ConnectionContext -> 'a option
static member ExecuteExactlyOne : cmd : Command<#IReadOnlyList<'a>> * conn : DbConnection -> 'a
static member ExecuteExactlyOne : cmd : Command<#IReadOnlyList<'a>> * context : ConnectionContext -> 'a
```

Note that it is not necessary to use these methods when RZSQL can statically infer that a query will always return exactly one row. For example, if you execute a simple `select 1 as x, 2 as y;` with no `where` or `limit` clause, that will always produce a single row, so RZSQL will generate the command type as `Command<Query.Row>` instead of `Command<IReadOnlyList<Query.Row>>`.

### Processing scalar results

When a generated row type has only one column, it will get an automatic implementation of `IScalar<TypeOfColumn>`. This allows you to use these extension methods for convenient handling of single-row, single-column queries like `select rowcount() as x;`.

```fsharp
static member ExecuteScalar : cmd : Command<#IScalar<'a>> * conn : DbConnection -> 'a
static member ExecuteScalar : cmd : Command<#IScalar<'a>> * context : ConnectionContext -> 'a
```

***

[← Rezoom.SQL](/rezoom-sql/api/rezoomsql.md) | [Rezoom.SQL.Asynchronous →](/rezoom-sql/api/rezoomsqlasynchronous.md)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://humbobst.gitbook.io/rezoom-sql/api/rezoomsqlsynchronous.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
