Skip to main content

Overview

MasterLabel uses ADO.NET with System.Data.SqlClient to connect to SQL Server. The connection string is configured in appsettings.json and can be overridden per environment.

Configuration Structure

The connection string is defined in the ConnectionStrings section of your configuration file:
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "SqlConnection": "Server=(localdb)\\mssqllocaldb;Database=MasterLabelDB;Trusted_Connection=True;"
  }
}
The connection string key must be named SqlConnection as this is referenced throughout the application code.

Environment-Specific Configuration

Development (LocalDB)

For local development using SQL Server LocalDB: appsettings.Development.json
{
  "ConnectionStrings": {
    "SqlConnection": "Server=(localdb)\\mssqllocaldb;Database=MasterLabelDB;Trusted_Connection=True;"
  }
}
LocalDB is automatically installed with Visual Studio and is perfect for development. No separate SQL Server installation required.

Production Configurations

{
  "ConnectionStrings": {
    "SqlConnection": "Server=SQLSERVER01;Database=MasterLabelDB;Integrated Security=True;TrustServerCertificate=True;"
  }
}

Connection String Parameters

Common connection string parameters used by MasterLabel:
ParameterDescriptionExample
ServerSQL Server instance name or address(localdb)\\mssqllocaldb, localhost, 192.168.1.100
DatabaseDatabase nameMasterLabelDB
Trusted_ConnectionUse Windows AuthenticationTrue or False
Integrated SecurityAlternative to Trusted_ConnectionTrue or SSPI
User IdSQL Server login usernamemasterlabel_app
PasswordSQL Server login passwordYourPassword
TrustServerCertificateSkip certificate validation (dev/test only)True or False
EncryptEncrypt the connectionTrue or False
Connection TimeoutConnection timeout in seconds30
For production environments using SQL Server 2022 or Azure SQL, encryption is enabled by default. Add TrustServerCertificate=True for self-signed certificates or Encrypt=False for non-encrypted connections (not recommended).

Security Best Practices

1

Use Environment Variables

Store sensitive connection strings in environment variables instead of configuration files:
# Set environment variable
setx ConnectionStrings__SqlConnection "Server=PROD01;Database=MasterLabelDB;User Id=app_user;Password=SecurePass123"
ASP.NET Core automatically reads connection strings from environment variables using the __ (double underscore) separator.
2

Use User Secrets in Development

For local development, store connection strings in User Secrets:
dotnet user-secrets init
dotnet user-secrets set "ConnectionStrings:SqlConnection" "Server=localhost;Database=MasterLabelDB;User Id=dev_user;Password=DevPass123"
3

Use Azure Key Vault or Configuration Providers

For production deployments, integrate with secure secret management:
  • Azure Key Vault
  • AWS Secrets Manager
  • HashiCorp Vault
  • Azure App Configuration
Never commit connection strings with passwords to source control. Use .gitignore to exclude appsettings.Production.json and other environment-specific configuration files containing credentials.

Verifying Configuration

To verify your connection string is working, you can test it using sqlcmd:
# Test LocalDB connection
sqlcmd -S "(localdb)\\mssqllocaldb" -d MasterLabelDB -E -Q "SELECT DB_NAME()"

# Test SQL Server with Windows Auth
sqlcmd -S SQLSERVER01 -d MasterLabelDB -E -Q "SELECT DB_NAME()"

# Test SQL Server with SQL Auth
sqlcmd -S SQLSERVER01 -d MasterLabelDB -U masterlabel_app -P YourPassword -Q "SELECT DB_NAME()"

Troubleshooting

Common Connection Issues

“Cannot open database “MasterLabelDB” requested by the login”
  • Ensure the database exists
  • Verify the user has permissions on the database
“A connection was successfully established, but then an error occurred during the login process”
  • Add TrustServerCertificate=True to your connection string for SQL Server 2022+
“Login failed for user”
  • Verify SQL Server authentication is enabled (for SQL Auth)
  • Check username and password are correct
  • Ensure the user exists and has proper permissions
MasterLabel uses the System.Data.SqlClient NuGet package (v4.9.0) for database connectivity. This is compatible with .NET 8.0 and all modern SQL Server versions.

Build docs developers (and LLMs) love