1

I have this

var d = $"'$.{ propertyName}'";
var propertyNameParam = new SqlParameter("@propertyName", d);
 var sql = "select * FROM Items WHERE JSON_VALUE(Attributes, @propertyName) like '%5151515151%'";

   count = dbContext.Items.FromSql(sql, propertyNameParam).Count();

but I get this error

System.Data.SqlClient.SqlException: 'The argument 2 of the "JSON_VALUE or JSON_QUERY" must be a string literal.'

but I put it in single quotes in the first line.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
chobo2
  • 83,322
  • 195
  • 530
  • 832
  • 1
    [C# Dapper using JSON_VALUE for SQL Server 2016](https://stackoverflow.com/a/46875662/5070879) - it could be variable but you need SQL Server 2017/Azure SQL DB – Lukasz Szozda Jul 02 '19 at 19:50
  • So only way to do it in sql server 2016 I got to do by string concatenation? – chobo2 Jul 02 '19 at 20:19
  • Yes, but you will expose your website to SQL Injection – Lukasz Szozda Jul 02 '19 at 20:21
  • but in 2017, I would be ok right with what I wrote? Till I wrote this post I was wondering why I was getting this error as I thought this area worked. Now I see that the servers I am deploying too have 2016 and I been using 2017....sigh – chobo2 Jul 02 '19 at 20:29
  • "but in 2017, I would be ok right?" In my link there are demos https://dbfiddle.uk/?rdbms=sqlserver_2016&fiddle=89795ded27ec1122a9061d249ad81860 vs https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=89795ded27ec1122a9061d249ad81860 Same code different version – Lukasz Szozda Jul 02 '19 at 20:30

0 Answers0