sqlx-mssql-odbc
Microsoft SQL Server driver for SQLx via ODBC.
This crate connects SQLx to Microsoft SQL Server through an ODBC driver manager
(unixODBC on Linux/macOS, built-in on Windows). It depends only on crates
published to crates.io and examples use sqlx-core directly.
Minimal Query
[]
= "0.1.4"
= "=0.9.0"
= { = "1", = ["macros", "rt-multi-thread"] }
use Connection;
use Row;
use MssqlConnection;
async
MssqlConnection::connect() accepts:
- A standard
mssql://URL:mssql://user:password@host:port/database?params - A raw ODBC connection string:
Driver={ODBC Driver 18 for SQL Server};Server=... - A bare DSN name:
MyMssqlDSN
URL query parameters
| Parameter | Description |
|---|---|
encrypt=true |
Enable TLS encryption (Encrypt=yes) |
trust_certificate=true |
Skip certificate validation (TrustServerCertificate=yes) |
driver=... |
Custom ODBC driver name (default: {ODBC Driver 18 for SQL Server}) |
Builder methods
let mut options = from_str?;
options.encrypt
.trust_certificate
.batch_size
.statement_cache_capacity;
let conn = options.connect.await?;
ODBC Setup
ODBC uses two native pieces outside this crate:
- A driver manager - unixODBC on Linux/macOS (built into Windows).
- The Microsoft ODBC Driver for SQL Server - version 17 or 18.
Enable the vendored-unix-odbc feature to statically link the unixODBC driver
manager into your application on Linux or macOS.
Linux (Debian/Ubuntu)
|
ACCEPT_EULA=Y
macOS
Windows
The ODBC driver manager is built into Windows. Install the Microsoft ODBC Driver for SQL Server.
Platform note: CI currently verifies Linux only. macOS and Windows are expected to work but are not exercised by automated tests. Community PRs for platform-specific fixes are welcome.
Connection Pooling
use MssqlPoolOptions;
let pool = new
.max_connections
.connect
.await?;
let row = query
.fetch_one
.await?;
Features
| Feature | Description |
|---|---|
bigdecimal |
BigDecimal type support |
chrono |
chrono datetime types |
rust_decimal / decimal |
Decimal type support |
json |
serde_json::Value support |
time |
time crate datetime types |
uuid |
uuid::Uuid support |
offline |
Compile-time query checking with query!() |
macros |
query!(), query_as!() and other proc macros |
derive |
Encode, Decode, Type, FromRow derive macros |
runtime-tokio |
Tokio runtime support |
tls-none |
No TLS (default) |
vendored-unix-odbc |
Statically link unixODBC |
CLI (sqlx-mssql)
A thin wrapper around sqlx-cli for managing MSSQL databases, running
migrations, and preparing offline query data.
Install
After installation, the sqlx-mssql binary is available on your PATH.
Usage
All standard sqlx-cli subcommands are supported. Provide your database URL
via --database-url or the DATABASE_URL environment variable (or a .env
file).
# Create / drop the database
# Create and run migrations
# Revert the last migration
# List migration status
# Prepare offline query data (for compile-time checked queries)
Environment variable (add to .env in your project root):
DATABASE_URL=mssql://sa:Password1!@localhost:1433/my_database
Or use the --database-url flag:
Run without installing
Running Tests
MSSQL_DATABASE_URL="mssql://sa:MyPass@localhost:1433/testdb" \
MSSQL_TEST_REQUIRED=1 \