0

Whilst working with PetaPoco in our development environment, we wrote a Fetch command a little like this:

DB.Fetch<object>("SELECT Col1, Col2 FROM Tablename WHERE Col1 = @0, parameter)

The resultant command looks a lot like this:

SELECT Col1, Col2 FROM Tablename WHERE Col1 = parameter

In most database versions, this is fine, however when the target database was moved to an environment running SQL Server 2014, it becomes apparent that this isn't good enough any more - SQL Server 2014 enforces the requirement to wrap varchar/string based parameters in single quotes, whereas previous versions were able to make a type determination at the point the query is executed and this was never an issue.

I can't reproduce the issue, as I don't have a SQL Server 2014 environment to hand, but it's apparent that SQL is trying to parse what it thinks is an integer to a string and comes out with what looks like a Hex value. Not a particularly helpful indication of what's happening, I grant you

The logical conclusion would be to adapt our Fetch to look something like this:

DB.Fetch<object>("SELECT Col1, Col2 FRMO Tablename WHERE Col1 = '@0', parameter)

Annoyingly, our last command looks like this:

SELECT Col1, Col2 FROM Tablename WHERE Col1 = '@0'

I've tried a number of different ways of writing my fetch command, including using a SqlParameter object as the first argument in the parameter array, defining the type as varchar, but I cannot get the query to work as I'd expect. The closest I have come is to use either string.Format, or an interpolated string, but this poses an obvious security risk and I'd rather avoid it.

I found this question which seems to suggest that the 'correct' thing to do is to remove the single quotes, however as I say, this is a breaking behaviour in MSSQL 2014+...

Is this not a supported feature of PetaPoco, or am I missing something?

Community
  • 1
  • 1
Ashilta
  • 173
  • 1
  • 10
  • What is the error you get? – Icepickle May 02 '17 at 09:02
  • I would actually be very surprised if it didn't work by default without the quotes so without you being able to reproduce the issue, it is very hard to find a reason. However, you could help in making it easier to reproduce for us if you would have an example of a connection string and how you create the database, and a simple table that represents a structure that would need this querying, without an MVCE it is very hard to decide if the problem is on your side or on the PETAPOCO side – Icepickle May 02 '17 at 10:14
  • As an extra info, what do you send as `parameter`? – Icepickle May 02 '17 at 15:07
  • 2
    PetaPoco maintainer here. You don't need quotes when using parameters, as the db engine will use the parameters directly. What happens when you do something like `DB.Fetch("SELECT @0", "test")`? – Plebsori May 03 '17 at 23:15

0 Answers0