sqlx-sqlserver 0.0.5

Independent Microsoft SQL Server driver crate for SQLx.
Documentation

sqlx-sqlserver

Independent Microsoft SQL Server driver crate for SQLx.

This crate is a standalone top-level crate. It depends only on crates published to crates.io and is not wired into the sqlx facade crate, so examples use sqlx-core directly.

Minimal Query

[dependencies]
sqlx-sqlserver = "0.0.5"
sqlx-core = "=0.9.0"
tokio = { version = "1", features = ["macros", "rt"] }
use sqlx_core::connection::{ConnectOptions, Connection};
use sqlx_core::row::Row;
use sqlx_sqlserver::MssqlConnectOptions;

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let mut conn = "mssql://sa:Password123!@localhost:1433/master?encrypt=mandatory&trust_server_certificate=true"
        .parse::<MssqlConnectOptions>()?
        .connect()
        .await?;

    let row = sqlx_core::query::query("SELECT 1")
        .fetch_one(&mut conn)
        .await?;

    let value: i32 = row.try_get(0)?;
    println!("{value}");

    conn.close().await?;
    Ok(())
}

Use fetch to read rows as SQL Server sends them. Dropping the stream between rows leaves the connection available for reuse. Before sending the next query, the driver drains the rest of the batch and applies its transaction changes. fetch_one and fetch_optional read to the end of the batch so they can report an error that follows the first row.

Any Driver

Install this crate explicitly before opening SQL Server URLs through SQLx AnyConnection:

use sqlx_core::connection::Connection;

sqlx_core::any::driver::install_drivers(&[sqlx_sqlserver::any::DRIVER])?;

let mut conn = sqlx_core::any::AnyConnection::connect(
    "mssql://sa:Password123!@localhost:1433/master?encrypt=mandatory&trust_server_certificate=true",
)
.await?;

For local development, run fast tests with ./scripts/ci.sh. Run e2e tests with MSSQL_DATABASE_URL=... ./scripts/test-mssql.sh.