使用 SqlConnection.GetSchema 仅获取表(无视图) [英] Use SqlConnection.GetSchema to get Tables Only (No Views)
问题描述
当我使用
SqlConnection.GetSchema("Tables");
它返回目标数据库的所有表和视图.
it returns all the tables AND views for the target database.
有没有办法只返回表?我所做的所有研究表明我这样做是正确的,但它总是返回像表格一样的视图.我在调试中深入研究了数据表,我什至找不到区别.报告的数据类型相同......据我所知,它无法区分视图和表.(这在某种程度上确实有意义,因为所有意图和目的的视图都是一个表格.)
Is there any way to just return tables? All the research I have done indicates I am doing this correctly yet it always returns the views like they are tables. I have dug down into the DataTable in debug and I can't even find a difference. The data types are reported the same... As far as I can tell, it can't differentiate between a view and a table. (It does make sense in a way since a view for all intents and purposes IS a table.)
我使用 Northwind 数据库进行测试.
I am using the Northwind database for testing.
我正在用 C# 编写应用程序.
I am writing the application in C#.
这是我运行以获取架构信息的代码.很简单.
Here is the code that I am running to get the schema info. Pretty simple.
SQLCon.Open();
DataTable tables = SQLCon.GetSchema("Tables");
SQLCon.Close();
我非常希望使用 getschema 方法,而不必以其他方式查询数据库......如果可能的话.
I would very much like to use the getschema method and not have to query the database in another fashion... if at all possible.
推荐答案
根据 这篇文章,返回的数据表有一列table_type
,它告诉你是VIEW
还是BASE TABLE
.
使用该列过滤掉 C# 端的视图.
According to this article, the returned data table has a column table_type
, which tells you whether it's a VIEW
or a BASE TABLE
.
Use that column to filter out the views on your C# end.
这篇关于使用 SqlConnection.GetSchema 仅获取表(无视图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!