C# 确定 SQL Server CE 中是否存在表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/431511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Determine if table exists in SQL Server CE?
提问by Michael Itzoe
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.
我知道这类似于这个问题,但我在 C# 中使用 SQL Server CE 3.5 和 WinForms 项目。如何判断表是否存在?我知道IF
不支持该关键字,但支持EXISTS
。信息模式是否存在于 CE 中,我可以在其中查询它?谢谢。
采纳答案by Mehrdad Afshari
Yes, it does exist:
是的,它确实存在:
SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'TableName'
回答by chrisgl
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.
一些简单的性能测试比针对 INFORMATION_SCHEMA 的查询有更好的结果。虽然我认为针对 INFORMATION_SCHEMA 的查询更干净。
回答by Alex Jolig
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
}