I've a problem when trying to convert my sql-query to a SqlCommand.
The query:
SELECT L1.[App_Name],
DATEDIFF(ss,MAX(L1.MESG_DATESTAMP),GETDATE()) as Seconds
FROM sbyn_csf.CSF_CME_LOG AS L1
JOIN sbyn_csf.CSF_LOGGER_LOG AS L2 ON L2.MESG_ID = L1.MESG_ID
WHERE logger_code = 101
AND [App_name] in ('002','010','022','025','036','037','040')
GROUP BY l1.[APP_NAME], L2.LOGGER_CODE
ORDER BY 1, 2
The problem is with the "in" operator, I want to build it dynamically in the program, whilst I'm able to change the query via a config-file.
private SqlCommand CreateSQLCommand(int i)
{
SqlCommand SqlCmd = new SqlCommand();
//SqlCmd.CommandText = SqlStrSelectTime;
SqlCmd.CommandText =
"SELECT L1.[App_Name]," + Environment.NewLine +
"DATEDIFF(ss,MAX(L1.MESG_DATESTAMP),GETDATE()) as Seconds" + Environment.NewLine +
"FROM sbyn_csf.CSF_CME_LOG AS L1" + Environment.NewLine +
"JOIN sbyn_csf.CSF_LOGGER_LOG AS L2 ON L2.MESG_ID = L1.MESG_ID" + Environment.NewLine +
"WHERE logger_code = @logger_code" + Environment.NewLine +
"AND [App_name] in (@inAppId)" + Environment.NewLine +
"GROUP BY l1.[APP_NAME], L2.LOGGER_CODE" + Environment.NewLine +
"ORDER BY 1, 2 ";
SqlCmd.Parameters.Add("@logger_code", SqlDbType.VarChar).Value = i;
List<string> inAppId = new List<string>();
//string inAppId = "''";
inAppId.Add("''");
for (int j = 0; j < ServicesList.Count; j++)
{
inAppId.Add(",'" + ServicesList[j].App_Name + "'");
//inAppId += ",'" + ServicesList[j].App_Name + "'";
}
SqlCmd.Parameters.Add("@inAppId", SqlDbType.VarChar).Value = inAppId;
return SqlCmd;
}
I'm grateful for any help!