Comment puis-je déterminer par programmation si une table existe dans une base de données SQL Server CE?

En arrière quand je n'avais qu'une table dans ma .sdf fichier, ce code a bien fonctionné:

const string sdfPath = @"\Program Files\duckbilled\Platypus.sdf";
string dataSource = string.Format("Data Source={0}", sdfPath);

if (!File.Exists(sdfPath))
{
    using (var engine = new SqlCeEngine(dataSource))
    {
        engine.CreateDatabase();
    }
    using (var connection = new SqlCeConnection(dataSource))
    {
        connection.Open();
        using (var command = new SqlCeCommand())
        {
            command.Connection = connection;
            command.CommandText =
                "CREATE TABLE Platydudes (Id int NOT NULL, BillSize smallint NOT NULL, Description nvarchar(255)";
            command.ExecuteNonQuery();
        }
    }
}

...mais maintenant j'ai besoin de savoir, non pas si le fichier de base de données (Platypus.sdf) existe, mais si un particulier table (comme Platydudes) existe dans la table/du fichier. Est-il un moyen de déterminer qui?

Mise à JOUR

Le "SI n'EXISTE PAS de" clause dans la requête provoque une exception d'exécution. Ce code:

using (var connection = new SqlCeConnection(dataSource))
{
    connection.Open();
    using (var command = new SqlCeCommand())
    {
        command.Connection = connection;
        command.CommandText = "IF NOT EXISTS( SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'InventoryItems') " +
            "CREATE TABLE InventoryItems (Id nvarchar(50) NOT NULL, PackSize smallint NOT NULL, Description nvarchar(255), DeptDotSubdept numeric, UnitCost numeric, UnitList numeric, UPCCode nvarchar(50), UPCPackSize smallint, CRVId int);";
        command.ExecuteNonQuery();
    }
}

...les causes de cette exception: Il y avait une erreur lors de l'analyse de la requête. [ Jeton numéro de ligne = 1, Jeton de décalage de ligne = 1, Jeton dans l'erreur = SI ]

Alors, évidemment, le "SI" l'entreprise n'est pas souhaité par le parseur. Est-il une autre façon de ne créer la table si elle n'existe pas déjà? Ou devrais-je, à chaque fois, d'abord de supprimer la table, puis de le recréer? OIE, dois-je faire ceci:

using (var connection = new SqlCeConnection(dataSource))
{
    connection.Open();
    using (var command = new SqlCeCommand())
    {
        command.Connection = connection;
        command.CommandText = "DELETE InventoryItems";
        command.ExecuteNonQuery();
    }
    using (var command = new SqlCeCommand())
    {
        command.Connection = connection;
        command.CommandText = "CREATE TABLE InventoryItems (Id nvarchar(50) NOT NULL, PackSize smallint NOT NULL, Description nvarchar(255), DeptDotSubdept numeric, UnitCost numeric, UnitList numeric, UPCCode nvarchar(50), UPCPackSize smallint, CRVId int);";
        command.ExecuteNonQuery();
    }
}

?

Mise à JOUR 2

Pour répondre à ma question ci-dessus dans la première mise à jour: NAN! Si je le fais, j'ai "Le tableau spécifié existe déjà" sur le second appel .ExecuteNonQuery().

Mise à JOUR 3

En réponse à Shiva commentaire ma réponse:

Ce (réutilisation de l'objet de commande) ne répond pas de la même façon ("table existe déjà"):

using (var command = new SqlCeCommand())
{
    command.Connection = connection;
    command.CommandText = "DELETE InventoryItems";
    command.ExecuteNonQuery();
    command.CommandText = "CREATE TABLE InventoryItems (Id nvarchar(50) NOT NULL, PackSize smallint NOT NULL, Description nvarchar(255), DeptDotSubdept numeric, UnitCost numeric, UnitList numeric, UPCCode nvarchar(50), UPCPackSize smallint, CRVId int);";
    command.ExecuteNonQuery();
}
DELETE InventoryItems ne supprime pas le tableau, c'est juste supprime toutes les lignes. DROP TABLE InventoryItems aurait pour effet de supprimer la table elle-même.
Oh, oui, je le savais; gel de cerveau!

OriginalL'auteur B. Clay Shannon | 2013-11-27