用c#怎么查询sql数据库中存不存在某张表

用c#怎么查询sql数据库中存不存在某张表,第1张

#region 判断数据库是否存在,通过指定专用的连接字符串,执行一个不需要返回值的SqlCommand命令。

/// <summary>

/// 判断数据库表是否存在,返回页头,通过指定专用的连接字符串,执行一个不需要返回值的SqlCommand命令。

/// </summary>

/// <param name="tablename">bhtsoft表</param>

/// <returns></returns>

public static bool CheckExistsTable(string tablename)

{

String tableNameStr = "select count(1) from sysobjects where name = '" + tablename + "'"

using (SqlConnection con = new SqlConnection(ConnectionString))

{

con.Open()

SqlCommand cmd = new SqlCommand(tableNameStr, con)

int result = Convert.ToInt32(cmd.ExecuteScalar())

if (result == 0)

{

return false

}

else

{

return true

}

}

}

#endregion

1.sql语句判断数据库表是否存在:sql:select * from user_all_tables wheretable_name='tableName'String helperName=delegator.getGroupHelperName("com.asiainfo")SQLProcessor sqlProcessor= new SQLProcessor(helperName)String sql = "select * from user_all_tables wheretable_name='"+table+"'"ResultSet rsTables=sqlProcessor.executeQuery(sql)if(rsTables.next()){Debug.logWarning("table:"+table+"exists",module)}else{Debug.logWarning("table:"+table+" does not exist", module)}方法二:DatabaseMetaData meta = m_sqlCon.getMetaData()ResultSet rsTables = meta.getTables(null , null,“YourTableName”, null)if(rsTables.next()){System.out.println("TheTable exsits.")}else{System.out.println("The如果schema参数为null的话,那么它会查询整个数据库中的表有可能会冲突的:getTables(String catalog,String schemaPattern,StringtableNamePattern,String[] types)参数: catalog:目录名称,一般都为空.参数:schema:数据库名,对于oracle来说就用户名参数:tablename:表名称参数:type :表的类型(TABLE | VIEW)注意:在使用过程中,参数名称必须使用大写的。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/10811433.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-11
下一篇2023-05-11

发表评论

登录后才能评论

评论列表(0条)

    保存