Visual Studio中C#查询Snowflake information_schema无结果,如何获取表列表
问题解决:C#读取Snowflake information_schema无结果的处理
可能原因及对应解决方案
1. 标识符大小写不匹配
Snowflake标识符(数据库、Schema名称)默认大小写不敏感,但如果创建时用双引号指定了大小写,必须严格匹配。可尝试两种调整方式:
- 用双引号包裹标识符:
string Query = "select TABLE_NAME from information_schema.tables where table_catalog = \"DB\" and table_schema = \"SCHEMA\";"; - 统一为小写(若Snowflake中对象是小写创建):
string Query = "select TABLE_NAME from information_schema.tables where table_catalog = 'db' and table_schema = 'schema';";
2. 连接上下文导致的范围偏差
如果C#连接默认指定了其他数据库/Schema,会导致information_schema查询范围不对。可在SQL中指定完整的information_schema路径:
string Query = "select TABLE_NAME from DB.information_schema.tables where table_catalog = 'DB' and table_schema = 'SCHEMA';";
3. 使用Snowflake .NET SDK的元数据API
直接通过官方Snowflake.Data.NET库的元数据获取方法,避免手写SQL的潜在问题:
using Snowflake.Data.Client; // 假设已初始化SnowflakeDbConnection实例conn var targetDb = "DB"; var targetSchema = "SCHEMA"; // 获取指定库和Schema下的表元数据 var tableMeta = conn.GetSchema("Tables", new string[] { null, targetDb, targetSchema }); // 遍历输出表名 foreach (System.Data.DataRow row in tableMeta.Rows) { Console.WriteLine(row["TABLE_NAME"].ToString()); }
4. 权限校验(低概率)
确认C#连接使用的Snowflake账号拥有目标information_schema的访问权限,执行以下语句(若需):
GRANT USAGE ON SCHEMA DB.INFORMATION_SCHEMA TO YOUR_ROLE;
替代方案:使用SHOW TABLES命令
Snowflake的SHOW TABLES命令可直接获取表列表,在C#中执行后读取结果:
string Query = "SHOW TABLES IN SCHEMA DB.SCHEMA;"; IDbCommand Cmd = Conn.CreateCommand(); Cmd.CommandText = Query; IDataReader Reader = Cmd.ExecuteReader(); while (Reader.Read()) { // 表名位于结果集第2列(索引1),可根据实际输出调整索引 Console.WriteLine(Reader.GetString(1)); }
内容的提问来源于stack exchange,提问作者Oscar Keshishyan
相关产品推荐
相关产品推荐

