查询以检查数据库是否为空(其中没有表) [英] query to check if database is empty(No Tables in it )

查看:437
本文介绍了查询以检查数据库是否为空(其中没有表)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮我写一下查询:我需要ac#查询来检查数据库是否为空。



我先检查是否有数据库存在:



Can anyone please help me to write the query: I need a c# query to check whether the database is empty or not.

I check first whether that database exists :

cmd.CommandText = "SELECT dbid FROM master.dbo.sysdatabases where name = '" + databaseName + "'";       
object DbID = cmd.ExecuteScalar();
if(DbID != null) //if database exists 
{
//CHECK IF DATABASE NOT EMPTY , if not empty ,display message already exists.                          
   cmd.CommandText = "select count(*) from INFORMATION_SCHEMA.TABLES";    
   //this is what i have tried but its giving same count for all the database even if there is a empty db.
   object TCount = cmd.ExecuteScalar();
   if(Convert.ToInt32(TCount) > 0) 
   //if database is not empty
   {
         //return result -database exists;
   }
}

推荐答案

尝试:

Try:
SELECT COUNT(name) FROM sys.Tables


这篇关于查询以检查数据库是否为空(其中没有表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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