I get InvalidCastException when I call First() at the last line. Can anyone tell me why?
static bool TableExists(string tableNameAndSchema)
{
string checkTable =
String.Format(
"IF OBJECT_ID('{0}', 'U') IS NOT NULL SELECT 'true' ELSE SELECT 'false'",
tableNameAndSchema);
var result = db.ExecuteQuery<bool>(checkTable);
return result.First();
}
Both of the following two formats work properly. The core of the challenge appears to be what type is returned by ExecuteQuery.
SELECT ‘true’ actually returns a string and not a bool – thus the ExecuteQuery result variable also has to be string (and not bool) if using that format
And when using SELECT 1 as suggested by @Eben Roux and @Anders Abel, then the ExecuteQuery result variable has to be int (and not bool)
Thanks to @Eben Roux and @Anders Abel whose answers pointed me toward the final solution(s) – (I marked your answers as useful as they steered me in the right direction to get the final answer – thanks guys 🙂 )