1

I have the following code:

long[] conIds = GetIDs();
dc.ExecuteCommand("UPDATE Deliveries SET Invoiced = NULL WHERE ID IN {0}",conIds);

I'd expect that to work, but the executecommand statement throws an exception stating that

A query parameter cannot be of type int64[].

Why is this?

Amar Palsapure
  • 9,590
  • 1
  • 27
  • 46
Chris
  • 7,415
  • 21
  • 98
  • 190

3 Answers3

1

You are passing an array of longs into a placeholder where a string is expected. You need to convert the array into a concatenated string with comma delimiters. Then, pass that into the command text.

Depending on where else you are using the GetIDs method, you could simplify your code by having that method return an array of strings. Then you can easily use String.Join here to convert that array to the concatenated text that you want.

DOK
  • 32,337
  • 7
  • 60
  • 92
0

I had trouble with this and I found that Linq is very particular about parameters. I had to build the SQL statement as a string and pass that string to .ExecuteCommand() for it to work. There was no error, just no results. Confusing.

That said has anyone seen a good solid method of viewing what Linq is passing to Sql Server?

I've used the SQL profiler but it's a tad vague.

RoadRunner
  • 31
  • 3
  • The DataContext.Log property lets you specify a TextWriter to log queries to. E.g. You can output it to the Console with `DataContext.Log = Console.Out` – Zac Faragher Sep 07 '17 at 01:07
0

You can't do this. SQL parameters must be single values.

You can make this work by converting your array to a list of comma separated values.

See here for an example.

command.CommandText = command.CommandText.Replace(
  "@conIDs", 
  string.Join(conIDs.Select(b => b.ToString()), ",")
);

In general, it's better to serialize your SQL and use a table valued parameter for this sort of thing. See this question for instructions.

Community
  • 1
  • 1
Kris Harper
  • 5,672
  • 8
  • 51
  • 96