I am attempting to use SQLParamaters to construct my SQL command. It works fine when I use them in the “where” clause but fail when used as part of the schema name. Here is the sample SQL string with the code for the problematic parameter. I’ve not shown the code for the @lastId parameter as this works and is the same as the code below.
The SQL string:
select * from [Exchequer].[@companyId].[CUSTSUPP] where acCode = @lastId
Code used to insert the companyId parameter:
var cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
var param = cmd.CreateParameter();
param.SqlDbType = SqlDbType.Text;
param.ParameterName = "@companyId";
param.Value = companyId;
cmd.Parameters.Add(param);
var reader = cmd.ExecuteReader();