Connecting to SQL Server with DataProvider
· DataProvider Team
DataProvider targets SQL Server as a first-class runtime. This guide shows you how to install the package, generate your first extension method, and call it safely.
Setup
Install the SQL Server runtime package plus the DataProvider CLI tool:
dotnet add package Nimblesite.DataProvider.SqlServer --version 0.9.6-beta
dotnet new tool-manifest
dotnet tool install DataProvider --version 0.9.6-beta
Generate from a query file
Write GetCustomers.sql:
SELECT Id, Name, Email
FROM Customers
WHERE Active = @active
Add it to DataProvider.json:
{
"connectionString": "Server=localhost;Database=MyDb;Trusted_Connection=true",
"queries": [
{ "name": "GetCustomers", "sqlFile": "GetCustomers.sql" }
]
}
Generate the extension methods:
dotnet DataProvider sqlserver --project-dir . --config DataProvider.json --out ./Generated
Call the generated method
using Microsoft.Data.SqlClient;
using Nimblesite.DataProvider.Core;
using MyApp.Generated;
await using var connection = new SqlConnection("Server=localhost;Database=MyDb;Trusted_Connection=true");
await connection.OpenAsync();
var result = await connection.GetCustomersAsync(active: true);
if (result is Result<IReadOnlyList<GetCustomersRow>, SqlError>.Ok ok)
{
foreach (var customer in ok.Value)
Console.WriteLine($"{customer.Id}: {customer.Name}");
}
This is the default generator output — the success and failure branches are both visible in the return type so nothing silently throws on the query path. The code template is pluggable if you want a different shape.
Check out the full documentation for the end-to-end walkthrough.