Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Reliably check if a table exists
Message
From
21/05/2007 00:50:19
 
 
To
All
General information
Forum:
ASP.NET
Category:
ADO.NET
Title:
Reliably check if a table exists
Environment versions
Environment:
C# 2.0
Database:
MS SQL Server
Miscellaneous
Thread ID:
01227222
Message ID:
01227222
Views:
90
What is the most reliable method of testing if a table exists?
I am using SQL Server compact edition.

Currently doing this:
protected bool TableExists(string tableName)
{
    string sql =  "SELECT table_name FROM INFORMATION_SCHEMA.tables WHERE table_name = '" + tableName + "'";
    bool exists = false;
    using (SqlCeConnection cn = new SqlCeConnection(this.connectionString))
    {
        using (SqlCeCommand cm = new SqlCeCommand(sql, cn))
        {
            cn.Open();
            SqlCeResultSet r = cm.ExecuteResultSet(ResultSetOptions.Scrollable);
            exists = r.HasRows;
        }
    }
    return exists;
}
Just like to ensure I'm using the most reliable test
- Craig

"If you're not prepared to be wrong, you will never come up with anything original."
- Sir Ken Robinson
Next
Reply
Map
View

Click here to load this message in the networking platform