Skip to content

ExecuteSqlCommand injecting parameters #10956

Closed as duplicate
Closed as duplicate

Description

@Compufreak345

If I execute the following command:

 var sqlCreateUser = $"CREATE USER [{loginName}] FOR LOGIN [{loginName}]";
 this.Context.Database.ExecuteSqlCommand(sqlCreateUser);

It fails with the following exception:

2018/02/12 19:02:43.414 | Fatal | MaintenanceTasks |  | MaintenanceTasks Main Me
thod: An unhandled exception occured : Es ist ein unerwarteter Fehler aufgetrete
n. | System.Data.SqlClient.SqlException (0x80131904): '@p1' is not a valid login
 or you do not have permission.
   bei System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boole
an breakConnection, Action`1 wrapCloseInAction)
   bei System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateOb
ject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   bei System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlComman
d cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler,
 TdsParserStateObject stateObj, Boolean& dataReady)
   bei System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, Ru
nBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean fo
rDescribeParameterEncryption)
   bei System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdB
ehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 tim
eout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean
 describeParameterEncryptionRequest)
   bei System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBeha
vior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompleti
onSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean as
yncWrite, Boolean inRetry)
   bei System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSo
urce`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean
& usedCache, Boolean asyncWrite, Boolean inRetry)
   bei System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   bei Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(
IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictio
nary`2 parameterValues)
   bei Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteN
onQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)

   bei Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.ExecuteS
qlCommand(DatabaseFacade databaseFacade, RawSqlString sql, IEnumerable`1 paramet
ers)
   bei Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.ExecuteS
qlCommand(DatabaseFacade databaseFacade, FormattableString sql)
Fehlernummer (Error Number):15007,Status (State):1,Klasse (Class):16 HelpLink.Pr
odName: Microsoft SQL Server;HelpLink.ProdVer: 13.00.4466;HelpLink.EvtSrc: MSSQL
Server;HelpLink.EvtID: 15007;HelpLink.BaseHelpUrl: http://go.microsoft.com/fwlin
k;HelpLink.LinkId: 20476 | Program.Main => LoggingExtensions.LogAclError

This is because it seems to add parameters automatically, although they are not supported for this type of command. If I change this code to the following (quotes instead of brackets, also only works when using the command as variable and not inlining it), it works:

 var sqlCreateUser = $"CREATE USER \"{loginName}\" FOR LOGIN \"{loginName}\"";
 this.Context.Database.ExecuteSqlCommand(sqlCreateUser);

Further technical details

EF Core version: 2.0.1
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10 Enterprise
IDE: Visual Studio 2017 15.4
I am building the .Net Core-Project against .Net Framework 4.6.2

Activity

  1. anpete commented on Feb 13, 2018

    @anpete
    Contributor

    @Compufreak345

    I tried:

    var loginName = "Foo";
    var sqlCreateUser = $"CREATE USER [{loginName}] FOR LOGIN [{loginName}]";
    context.Database.ExecuteSqlCommand(sqlCreateUser);

    and we generated:

    CREATE USER [Foo] FOR LOGIN [Foo]

    which is expected.

  2. divega commented on Feb 16, 2018

    @divega
    Contributor

    @Compufreak345 I assume when you hit the issue you had the SQL string inline in the call. That will resolve to the interpolated string overload which does parameterize.

  3. ajcvickers commented on Feb 17, 2018

    @ajcvickers
    Contributor

    Closing as the issues with interpolated strings are being tracked elsewhere--e.g. #10996.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions