C# 查询SQL数据库中指定表是否存在

您所在的位置:网站首页 判断数据库表是否存在的方法 C# 查询SQL数据库中指定表是否存在

C# 查询SQL数据库中指定表是否存在

2024-07-09 04:17| 来源: 网络整理| 查看: 265

1.  该程序的connIBDealDBString是针对名为IBDealDB数据库的连接字符串

2. 参数:表名,数据库名:程序按指定的数据库查询是否存在该表,若存在返回true, 否则返回false

3. 程序利用ExecuteScalar()特性(返回结果的第一行,第一列),通过SQL语句去查询sysobjects来实现目的,因sysobjects数据量少,通过这种办法判断表是否存在,也较少的消耗资源

public static Boolean IsExistTable(string pTableName, string pDB_NAME) { SqlConnection connCheckTable = new SqlConnection(connIBDealDBString); if (connCheckTable.State != ConnectionState.Open) connCheckTable.Open(); string isExitTableSql = @"select * from " + pDB_NAME + @"..sysobjects where name = '" + pTableName + @"'"; cmd = new SqlCommand(isExitTableSql, connCheckTable); object pResult = cmd.ExecuteScalar(); if (pResult == null || pResult == System.DBNull.Value) return false; return true; }

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3