SQL Compact doesn't like much for logic inside a SQL statement, so Shiva's answer probably won't pass the parser. He's on the right course, though. You can to do it in two steps:
Here's the TableExists
method from the SQL Compact implementation of the OpenNETCF ORM:
public override bool TableExists(string tableName)
{
var connection = GetConnection(true);
try
{
using (var command = GetNewCommandObject())
{
command.Transaction = CurrentTransaction as SqlCeTransaction;
command.Connection = connection;
var sql = string.Format(
"SELECT COUNT(*) FROM information_schema.tables WHERE table_name = '{0}'",
tableName);
command.CommandText = sql;
var count = Convert.ToInt32(command.ExecuteScalar());
return (count > 0);
}
}
finally
{
DoneWithConnection(connection, true);
}
}
Obviously it's not complete for your case, but it's pretty easy to understand. CurrentTransaction
could easily be null. GetNewCommandObject
simply returns a new SqlCeCommand
instance. GetConnection
simply could return a new SqlCeConnection
instance. DoneWithConnection could be a NOP. Basiocally these are all handling the fact that the ORM supports a plethora of backing stores. The kernel of info you need is the SQL that I pass in and how I determine the true/false return.
My (untested in a compiler) guess to the resulting method would be something like this:
public bool TableExists(SqlCeConnection connection, string tableName)
{
using (var command = new SqlCeCommand())
{
command.Connection = connection;
var sql = string.Format(
"SELECT COUNT(*) FROM information_schema.tables WHERE table_name = '{0}'",
tableName);
command.CommandText = sql;
var count = Convert.ToInt32(command.ExecuteScalar());
return (count > 0);
}
}