我如何在C#中检查数据库(ACCESS或SQL)中是否存在表 [英] how can i check whether a table exists in the database (ACCESS or SQL) in C#

查看:982
本文介绍了我如何在C#中检查数据库(ACCESS或SQL)中是否存在表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了很多与此问题有关的问题.

I found a lot of questions regarding with this question.

但是有没有简单的语句可以完成此任务?

But is there any simple statements to accomplish this task?

对于SQL和访问权限

推荐答案

IF (EXISTS (SELECT 1 FROM sys.tables WHERE name = 'table_name'))
BEGIN
    -- do stuff
END

sys.tables 可以还为您提供有关表对象的一些信息,例如is_replicated列告诉您该表是通过复制创建的,还是has_replication_filter列告诉您该表是否设置了复制过滤器

sys.tables can also give you some information about the table object, e.g. the is_replicated column tells you if the table was created by replication or the has_replication_filter column tells you if the table has a replication filter set up

NB:这是用于SQL Server

NB: this is for SQL Server

用于访问:

SELECT COUNT(*) as Exists from MsysObjects 
WHERE type = 1
AND name = 'MY_TABLE_NAME' 

这篇关于我如何在C#中检查数据库(ACCESS或SQL)中是否存在表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆