System.ArgumentException: Keyword not supported: 'servername'. means one key in your
connection string is not a key Microsoft.Data.SqlClient knows. Here it is a typo for
Server. Npgsql reports the same mistake as
Couldn't set servername (Parameter 'servername'). Correct the key and the exception goes.
Use Server= for SQL Server and Host= for PostgreSQL, check the other keys
against the driver's own list, and build strings in code with the typed connection string builder so
a misspelled key cannot compile.
The error
Unhandled exception. System.ArgumentException: Keyword not supported: 'servername'.
at Microsoft.Data.SqlClient.SqlConnectionOptions.ParseInternal(Dictionary`2 parsetable, String connectionString, Boolean buildChain, IReadOnlyDictionary`2 synonyms, Boolean firstKey)
at Microsoft.Data.SqlClient.SqlConnectionOptions..ctor(String connectionString)
...
at Microsoft.Data.SqlClient.SqlConnection..ctor(String connectionString)
And the Npgsql wording for the same string:
Unhandled exception. System.ArgumentException: Couldn't set servername (Parameter 'servername')
---> System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
at Npgsql.NpgsqlConnectionStringBuilder.GeneratedActions(GeneratedAction action, String keyword, Object& value)
...
at Npgsql.NpgsqlConnection..ctor(String connectionString)
Both came from a string that starts with ServerName=:
var connectionString = "ServerName=(localdb)\\MSSQLLocalDB;Database=master;Integrated Security=true";
using var connection = new SqlConnection(connectionString);
Why it happens
Both drivers parse the whole string as soon as you assign it, in the constructor, and refuse any key
they do not recognise. The stack trace shows it: the exception is thrown from
SqlConnection..ctor, before Open() and before any network traffic, so no
database needs to be running to see it. The key is lower-cased in the message, which is why
ServerName appears as 'servername'.
The common causes are a typo, or a string copied from another driver. A SQL Server string pasted into a
PostgreSQL app fails the same way: Npgsql answered Initial Catalog=clinic with
Couldn't set initial catalog (Parameter 'initial catalog'), because PostgreSQL's key is
Database.
The fix
var connectionString = "Server=(localdb)\\MSSQLLocalDB;Database=master;Integrated Security=true";
With Server= the same program printed
Connected to (localdb)\MSSQLLocalDB, server version 16.00.1210. For Npgsql,
Host=127.0.0.1;Port=15899;Database=clinic;Username=clinic_app was accepted by the
constructor. Check every key against the official lists: the
SqlConnection.ConnectionString
reference and Npgsql's
connection string parameters.
When you assemble a string in code, use the builder's properties instead of text:
var builder = new SqlConnectionStringBuilder
{
DataSource = @"(localdb)\MSSQLLocalDB",
InitialCatalog = "master",
IntegratedSecurity = true
};
It produced Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True
and connected. Parsing the misspelled string with new SqlConnectionStringBuilder(...)
throws the same Keyword not supported at that line, which is a handy way to validate a
string read from configuration at startup. To check a string without running anything, paste it into
the connection string checker; for this one it reported
Unknown key for SQL Server (Microsoft.Data.SqlClient): ServerName and a missing Data Source.
How it was reproduced
A console project (dotnet new console) with Microsoft.Data.SqlClient 7.1.1 and Npgsql
10.0.3, on .NET SDK 10.0.403 with runtime 10.0.14, on Windows 11. No PostgreSQL server ran: the Npgsql
errors fire in the constructor. The fixed SqlClient string connected to SQL Server LocalDB 16.0.1210.
The database and user names are the test's own.
Frequently asked
- What does Keyword not supported mean in a SQL Server connection string?
- One of the keys is not a key Microsoft.Data.SqlClient knows, usually a typo such as ServerName for Server or a key from another driver. The message names the key in lower case.
- What is the Npgsql error for an unknown connection string key?
- Npgsql throws ArgumentException: Couldn't set servername (Parameter 'servername') with an inner KeyNotFoundException. It means the same thing as SqlClient's Keyword not supported.
- Does Keyword not supported mean the database is down?
- No. The string is parsed in the SqlConnection or NpgsqlConnection constructor, before any connection is attempted, so the error appears even when no database server is running.
More decoded errors in the Fixes category, including what to do when Npgsql hides a constraint's details in Detail redacted.