-
Notifications
You must be signed in to change notification settings - Fork 13
Check SQL Syntax
Denis Voituron edited this page Oct 5, 2025
·
1 revision
Call the method Mocks.HasValidSqlServerCommandText()
to check if your string CommandText respect the SQL Server syntax.
Without connection to SQL Server
(but using the (Microsoft.SqlServer.SqlParser)[https://www.nuget.org/packages/Microsoft.SqlServer.SqlParser] package).
conn.Mocks
.HasValidSqlServerCommandText()
.WhenTag("MyTag")
.ReturnsScalar(14);So the CommandText="SELECT ** FROM EMP" (double *)
will raised a MockException with the message
Incorrect syntax near '*'.
You can also define a default value using the
MockDbConnection.HasValidSqlServerCommandText property.
var conn = new MockDbConnection()
{
HasValidSqlServerCommandText = true
};If your database engine is SQL Server, we recommand to use this flag, to validate all your queries.
Even if you have disable syntax checking for all queries, you can enable it for a single query.
conn.Mocks
.When(cmd => cmd.CommandText.Contains("FROM EMP") &&
cmd.HasValidSqlServerCommandText() )
.ReturnsScalar(14);Contact me at Denis[at]Voituron.net