How to pass an array of integer separated by comma to an ExecuteStoreCommandin the entities
as a parameter
I am not able to execute this :
this.ObjectContext.ExecuteStoreCommand("INSERT INTO SurveyPatientListMrns
(UserData, MrnId) SELECT DISTINCT '{0}' , MrnId
FROM PatientVisits WHERE (FacilityId = {1})
AND (UnitId IN ({2}))", userData, facilityId, (string.Join(",", unitIds)));
Here (string.Join(",", unitIds)) is a string and i can not cast it as integer because of the commas. How can i pass the parameter then?
FYI, unitIds is a array of integers
Though it looks like a
string.Formatoperation,ExecuteStoreCommandis internally building a parameterized query to increase performance and help protect you from SQL injection attacks. (MSDN)When you do your
string.Joinas a parameter toExecuteStoreCommand, it treats that result not as a list of values for theINclause, but a string that just happens to look like one. Basically it will generate anINclause that looks like this:Which is obviously not what you want.
You’re going to have to build the SQL command with the
string.Join-ed list ofuinitIdsBEFORE passing itExecuteStoreCommand:Normally one should avoid dynamically building SQL queries because of the possibility of a SQL injection attack, but in this case, you know that
unitIdsis a list of integers, and therefore you should be OK.