In
my post yesterday I talked about the importance of not relying on the existence of a SQL Mobile database as an indication of a successful SQL Merge Replication initialization - to be sure that the replication completed, you must also verify that the table(s) exist.
I've had a few people ask how to check that a table exists in SQL Mobile. Basically you do it using the same INFORMATION_SCHEMA.TABLES view that you use with SQL Server 2005.
Here's an example function to check that the passed table name exists in the database opened on the passed connection.
private bool TableExists(SqlCeConnection conn, string tableName)
{
const string tableExistsSql =
"SELECT count(*) FROM INFORMATION_SCHEMA.TABLES where TABLE_NAME = @tableName";
bool tableExists = false;
if (conn != null)
{
using (SqlCeCommand cmd = new SqlCeCommand(tableExistsSql, conn))
{
cmd.Parameters.Add("@tableName", tableName);
int rowsFound = (int)cmd.ExecuteScalar();
tableExists = rowsFound > 0;
}
}
return tableExists;
}
Pretty Straightforward - One thing to note is that this function assumes that a null connection indicates that the database (and therefore the table) doesn't exist. This may not be the appropriate logic in your scenario so be sure to modify the function as needed. For example, you might want to modify the function to first check that the database file exists, then open the connection to the database file and then check for the table.
One thing I forgot to do in
yesterday's post - I'd like to thank Martin Schray at Microsoft for all of his help with the work I've been doing in this area. He's spent a lot of his time and has provided a lot of very helpful information.
Posted
Aug 08 2006, 03:18 PM
by
jim-wilson