I'm trying to execute CREATE USER with a given username (via sql parameter)
exec sp_executesql N'CREATE USER @LoginName
FOR LOGIN @LoginName;',
N'@LoginName varchar(5)', @LoginName='myuser'
Heres the code thats generating the above:
Dim myCommand As SqlCommand = New SqlCommand("CREATE USER @LoginName
FOR LOGIN @LoginName;",
ClassDatabaseConnection.CustomInstance)
myCommand.CommandType = CommandType.Text
myCommand.Parameters.Add("@LoginName", SqlDbType.VarChar).Value = LoginName
myCommand.ExecuteScalar()
I get and error:
Incorrect syntax near '@LoginName'.
I think this is due to the parameters being passed as VarChar causing 'MyUser' rather than MyUser
Can I not do this with sql parameters?