Determine if table exists in SQL Server CE?

2020-06-01 07:22发布

I know this is similar to this question, but I'm using SQL Server CE 3.5 with a WinForms project in C#. How can I determine whether a table exists? I know the IF keyword is not supported, though EXISTS is. Does information_schema exist in CE where I can query against it? Thanks.

3条回答
够拽才男人
2楼-- · 2020-06-01 07:36

As an alternative you can Query the Table and catch the Exception thrown. If there is an Exception, the Table wasn't found, else the Table exists.

SELECT TOP 1 1 FROM TableName;

A little and simple Performance Test had better Results than the Query against INFORMATION_SCHEMA. Although I would consider a Query against INFORMATION_SCHEMA as cleaner.

查看更多
混吃等死
3楼-- · 2020-06-01 07:48

Using Database helper:

var db = Database.Open("MyDatabase");
var sql = @"SELECT Count(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'MyTable'"
var count = db.QueryValue(sql);
if(count.Equals(1)){
    //table exists
}
查看更多
The star\"
4楼-- · 2020-06-01 07:52

Yes, it does exist:

SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'TableName'
查看更多
登录 后发表回答